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().
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',
}
})
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: |
|
---|
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.
Invalidate a cache region corresponding to a function decorated with cache_region().
Parameters: |
|
---|
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)
Front-end to the containment API implementing a data cache.
Parameters: |
|
---|
Retrieve a cached value from the container
Clear all the values from the namespace
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 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.
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)