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().
Front-end to the containment API implementing a data cache.
Parameters: |
|
---|
Clear all the values from the namespace
Retrieve a cached value from the container
Retrieve a cached value from the container
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)
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)
Container and Namespace classes
Implements synchronization and value-creation logic for a ‘value’ stored in a NamespaceManager.
Container and its subclasses are deprecated. The Value class is now used for this purpose.
Handles dictionary operations and locking for a namespace of values.
NamespaceManager provides a dictionary-like interface, implementing __getitem__(), __setitem__(), and __contains__(), as well as functions related to lock acquisition.
The implementation for setting and retrieving the namespace data is handled by subclasses.
NamespaceManager may be used alone, or may be accessed by one or more Value objects. Value objects provide per-key services like expiration times and automatic recreation of values.
Multiple NamespaceManagers created with a particular name will all share access to the same underlying datasource and will attempt to synchronize against a common mutex object. The scope of this sharing may be within a single process or across multiple processes, depending on the type of NamespaceManager used.
The NamespaceManager itself is generally threadsafe, except in the case of the DBMNamespaceManager in conjunction with the gdbm dbm implementation.
NamespaceManager that uses a Python dictionary for storage.
NamespaceManager that uses dbm files for storage.
NamespaceManager that uses binary files for storage.
Each namespace is implemented as a single file storing a dictionary of key/value pairs, serialized using the Python pickle module.
Deprecated.
Provides the NamespaceManager API over a memcache client library.
Container class which invokes MemcacheNamespaceManager.
Extends python cookie to give digital signature support
Session object that uses container package for storage.
Parameters: |
|
---|
Session proxy/lazy creator
This object proxies access to the actual session object, so that in the case that the session hasn’t been used before, it will be setup. This avoid creating and loading the session from persistent storage unless its actually used during the request.
Decode a Base64 encoded string.
s is the string to decode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the ‘+’ and ‘/’ characters.
The decoded string is returned. A TypeError is raised if s were incorrectly padded or if there are non-alphabet characters present in the string.
Encode a string using Base64.
s is the string to encode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies an alternative alphabet for the ‘+’ and ‘/’ characters. This allows an application to e.g. generate url or filesystem safe Base64 strings.
The encoded string is returned.
Synchronization functions.
File- and mutex-based mutual exclusion synchronizers are provided, as well as a name-based mutex which locks within an application based on a string name.
a proxy for an RLock object that is stored in a name based registry.
Multiple threads can get a reference to the same RLock based on the name alone, and synchronize operations related to that name.
Base class for a synchronization object that allows multiple readers, single writers.
A synchronizer which locks using flock().
a synchronizer using a Condition.
Beaker utilities
An efficient/threadsafe singleton map algorithm, a.k.a. “get a value based on this key, and create if not found or not valid” paradigm:
exists && isvalid ? get : create
Designed to work with weakref dictionaries to expect items to asynchronously disappear from the dictionary.
Use python 2.3.3 or greater ! a major bug was just fixed in Nov. 2003 that was driving me nuts with garbage collection/weakrefs in this section.
stores a value on a per-thread basis
verifies and creates a directory. tries to ignore collisions with other threads and processes.
Generate a unique file-accessible path from the given list of identifiers starting at the given root directory.