This module provides a simple caching mechanism for preventing unnecessary data transfers. Adding, removing and retrieving items will be carried out using dictionary operations.
The cache is either a regular dictionary or a file as handled by the anydbm module. If you would like to use such a file-based cache, you can specify the file name when initializing the cache:
c = Cache(os.path.expanduser('~/.humphrey.cache'))
There is an expiration time on the stored items (default is 1 hour). This can be specified (in seconds) at initialization. For example, to create a cache with a 1 minute expiration time:
c = Cache(expires=60)
Every item is stored along with a timestamp, which will be updated whenever the item is accessed. If you want to access an item without updating its timestamp, use:
item = c.get(key, default, update=False)
A simple cache which behaves like a dictionary.
| Parameters: |
|
|---|
Clear all expired items from the cache.
| Parameter: | force (bool) – Whether items will be deleted even if they have not expired. |
|---|
Return an item in the cache.
| Parameters: |
|
|---|