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
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.
Invalidate a cache region namespace or decorated function
This function only invalidates cache spaces created with the cache_region decorator.
| Parameters: |
|
|---|
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)
Front-end to the containment API implementing a data cache.
| Parameters: |
|
|---|
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.
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.
Invalidate a cache region namespace or decorated function
This function only invalidates cache spaces created with the cache_region decorator.
| Parameters: |
|
|---|
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)
Decorate a function to cache itself with supplied parameters
| Parameters: |
|
|---|
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 a cache decorated function
This function only invalidates cache spaces created with the cache decorator.
| Parameters: |
|
|---|
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)