Package trac :: Module cache

Module cache

source code

Classes
  CacheManager
Cache manager.
Functions
 
cached(fn_or_id=None)
Method decorator creating a cached attribute from a data retrieval method.
source code
Function Details

cached(fn_or_id=None)

source code 
Method decorator creating a cached attribute from a data retrieval
method.

Accessing the cached attribute gives back the cached value. The data
retrieval method is called as needed by the CacheManager. Invalidating
the cache for this value is done by `del`eting the attribute.

The data retrieval method is called with a single argument `db` containing
a reference to a database connection. All data retrieval should be done
through this connection.

Note that the cache validity is maintained using a table in the database.
Cache invalidation is performed within a transaction block, and can be
nested within another transaction block.

The id used to identify the attribute in the database is constructed from
the names of the containing module, class and retriever method. If the
decorator is used in non-signleton (typically non-`Component`) objects,
an optional string specifying the name of the attribute containing the id
must be passed to the decorator call as follows:
{{{
def __init__(self, env, name):
    self.env = env
    self._metadata_id = 'custom_id.' + name

@cached('_metadata_id')
def metadata(db):
    ...
}}}

This decorator requires that the object on which it is used has an `env`
attribute containing the application `Environment`.