beaker.cache – Cache module

This package contains the “front end” classes and functions for Beaker caching.

Included are the Cache and CacheManager classes, as well as the function decorators region_decorate(), region_invalidate().

Module Contents

beaker.cache.cache_regions = {}

Dictionary of ‘region’ arguments.

A “region” is a string name that refers to a series of cache configuration arguments. An application may have multiple “regions” - one which stores things in a memory cache, one which writes data to files, etc.

The dictionary stores string key names mapped to dictionaries of configuration arguments. Example:

from beaker.cache import cache_regions
cache_regions.update({
    'short_term':{
        'expire':60,
        'type':'memory'
    },
    'long_term':{
        'expire':1800,
        'type':'dbm',
        'data_dir':'/tmp',
    }
})
beaker.cache.cache_region(region, *args)

Decorate a function such that its return result is cached, using a “region” to indicate the cache arguments.

Example:

from beaker.cache import cache_regions, cache_region

# configure regions
cache_regions.update({
    'short_term':{
        'expire':60,
        'type':'memory'
    }
})

@cache_region('short_term', 'load_things')
def load(search_term, limit, offset):
    '''Load from a database given a search term, limit, offset.'''
    return database.query(search_term)[offset:offset + limit]

The decorator can also be used with object methods. The self argument is not part of the cache key. This is based on the actual string name self being in the first argument position (new in 1.6):

class MyThing(object):
    @cache_region('short_term', 'load_things')
    def load(self, search_term, limit, offset):
        '''Load from a database given a search term, limit, offset.'''
        return database.query(search_term)[offset:offset + limit]

Classmethods work as well - use cls as the name of the class argument, and place the decorator around the function underneath @classmethod (new in 1.6):

class MyThing(object):
    @classmethod
    @cache_region('short_term', 'load_things')
    def load(cls, search_term, limit, offset):
        '''Load from a database given a search term, limit, offset.'''
        return database.query(search_term)[offset:offset + limit]
Parameters:
  • region – String name of the region corresponding to the desired caching arguments, established in cache_regions.
  • *args – Optional str()-compatible arguments which will uniquely identify the key used by this decorated function, in addition to the positional arguments passed to the function itself at call time. This is recommended as it is needed to distinguish between any two functions or methods that have the same name (regardless of parent class or not).

Note

The function being decorated must only be called with positional arguments, and the arguments must support being stringified with str(). The concatenation of the str() version of each argument, combined with that of the *args sent to the decorator, forms the unique cache key.

Note

When a method on a class is decorated, the self or cls argument in the first position is not included in the “key” used for caching. New in 1.6.

beaker.cache.region_invalidate(namespace, region, *args)

Invalidate a cache region corresponding to a function decorated with cache_region().

Parameters:
  • namespace – The namespace of the cache to invalidate. This is typically a reference to the original function (as returned by the cache_region() decorator), where the cache_region() decorator applies a “memo” to the function in order to locate the string name of the namespace.
  • region – String name of the region used with the decorator. This can be None in the usual case that the decorated function itself is passed, not the string name of the namespace.
  • args – Stringifyable arguments that are used to locate the correct key. This consists of the *args sent to the cache_region() decorator itself, plus the *args sent to the function itself at runtime.

Example:

from beaker.cache import cache_regions, cache_region, region_invalidate

# configure regions
cache_regions.update({
    'short_term':{
        'expire':60,
        'type':'memory'
    }
})

@cache_region('short_term', 'load_data')
def load(search_term, limit, offset):
    '''Load from a database given a search term, limit, offset.'''
    return database.query(search_term)[offset:offset + limit]

def invalidate_search(search_term, limit, offset):
    '''Invalidate the cached storage for a given search term, limit, offset.'''
    region_invalidate(load, 'short_term', 'load_data', search_term, limit, offset)

Note that when a method on a class is decorated, the first argument cls or self is not included in the cache key. This means you don’t send it to region_invalidate():

class MyThing(object):
    @cache_region('short_term', 'some_data')
    def load(self, search_term, limit, offset):
        '''Load from a database given a search term, limit, offset.'''
        return database.query(search_term)[offset:offset + limit]

    def invalidate_search(self, search_term, limit, offset):
        '''Invalidate the cached storage for a given search term, limit, offset.'''
        region_invalidate(self.load, 'short_term', 'some_data', search_term, limit, offset)
class beaker.cache.Cache(namespace, type='memory', expiretime=None, starttime=None, expire=None, **nsargs)

Front-end to the containment API implementing a data cache.

Parameters:
  • namespace – the namespace of this Cache
  • type – type of cache to use
  • expire – seconds to keep cached data
  • expiretime – seconds to keep cached data (legacy support)
  • starttime – time when cache was cache was
clear()

Clear all the values from the namespace

get(key, **kw)

Retrieve a cached value from the container

class beaker.cache.CacheManager(**kwargs)

Initialize a CacheManager object with a set of options

Options should be parsed with the parse_cache_config_options() function to ensure only valid options are used.

cache(*args, **kwargs)

Decorate a function to cache itself with supplied parameters

Parameters:
  • args – Used to make the key unique for this function, as in region() above.
  • kwargs – Parameters to be passed to get_cache(), will override defaults

Example:

# Assuming a cache object is available like:
cache = CacheManager(dict_of_config_options)


def populate_things():

    @cache.cache('mycache', expire=15)
    def load(search_term, limit, offset):
        return load_the_data(search_term, limit, offset)

    return load('rabbits', 20, 0)

Note

The function being decorated must only be called with positional arguments.

invalidate(func, *args, **kwargs)

Invalidate a cache decorated function

This function only invalidates cache spaces created with the cache decorator.

Parameters:
  • func – Decorated function to invalidate
  • args – Used to make the key unique for this function, as in region() above.
  • kwargs – Parameters that were passed for use by get_cache(), note that this is only required if a type was specified for the function

Example:

# Assuming a cache object is available like:
cache = CacheManager(dict_of_config_options)


def populate_things(invalidate=False):

    @cache.cache('mycache', type="file", expire=15)
    def load(search_term, limit, offset):
        return load_the_data(search_term, limit, offset)

    # If the results should be invalidated first
    if invalidate:
        cache.invalidate(load, 'mycache', 'rabbits', 20, 0, type="file")
    return load('rabbits', 20, 0)
region(region, *args)

Decorate a function to cache itself using a cache region

The region decorator requires arguments if there are more than two of the same named function, in the same module. This is because the namespace used for the functions cache is based on the functions name and the module.

Example:

# Assuming a cache object is available like:
cache = CacheManager(dict_of_config_options)


def populate_things():

    @cache.region('short_term', 'some_data')
    def load(search_term, limit, offset):
        return load_the_data(search_term, limit, offset)

    return load('rabbits', 20, 0)

Note

The function being decorated must only be called with positional arguments.

region_invalidate(namespace, region, *args)

Invalidate a cache region namespace or decorated function

This function only invalidates cache spaces created with the cache_region decorator.

Parameters:
  • namespace – Either the namespace of the result to invalidate, or the cached function
  • region – The region the function was cached to. If the function was cached to a single region then this argument can be None
  • args – Arguments that were used to differentiate the cached function as well as the arguments passed to the decorated function

Example:

# Assuming a cache object is available like:
cache = CacheManager(dict_of_config_options)

def populate_things(invalidate=False):

    @cache.region('short_term', 'some_data')
    def load(search_term, limit, offset):
        return load_the_data(search_term, limit, offset)

    # If the results should be invalidated first
    if invalidate:
        cache.region_invalidate(load, None, 'some_data',
                                'rabbits', 20, 0)
    return load('rabbits', 20, 0)