Table Of Contents

Previous topic

Supported Backends

Next topic

humphrey.core.catalog

This Page

humphrey.core.cache

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)

API

class humphrey.core.cache.Cache(filename=None, expires=3600)

A simple cache which behaves like a dictionary.

Parameters:
  • filename (str) – Name of the file that contains the cache. If not specified, a dictionary will be used.
  • expires (int) – Item expiry time (in seconds).
clear(force=False)

Clear all expired items from the cache.

Parameter:force (bool) – Whether items will be deleted even if they have not expired.
close()
Close the cache.
get(key, default=None, update=True)

Return an item in the cache.

Parameters:
  • key (str) – Key to search in the cache.
  • default – Default value to return if the key is not found in the cache.
  • update (bool) – Whether the last access time of the item will be updated.