Beaker

beaker.cache – Cache module

Cache object

The Cache object is used to manage a set of cache files and their associated backend. The backends can be rotated on the fly by specifying an alternate type when used.

Advanced users can add new backends in beaker.backends

Module Contents

beaker.cache.cache_region(region, *deco_args)

Decorate a function to cache itself using a cache region

The region decorator requires arguments if there are more than 2 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:

# Add cache region settings to beaker:
beaker.cache.cache_regions.update(dict_of_config_region_options))

@cache_region('short_term', 'some_data')
def populate_things(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.

beaker.cache.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 reference
  • 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:

# Add cache region settings to beaker:
beaker.cache.cache_regions.update(dict_of_config_region_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:
        region_invalidate(load, None, 'some_data',
                                'rabbits', 20, 0)
    return load('rabbits', 20, 0)
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
get(key, **kw)
Retrieve a cached value from the container
clear()
Clear all the values from the namespace
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.

region(region, *args)

Decorate a function to cache itself using a cache region

The region decorator requires arguments if there are more than 2 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 name of 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)
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)

Table Of Contents

Previous topic

Changes in Beaker

Next topic

beaker.container – Container and Namespace classes

This Page