API

class fcache.Cache(cachename, appname[, appauthor=None])[source]

Take cachename and appname, then create a cache file. If a matching cache file was found, a new one is not created.

appauthor is used on Windows to determine the appropriate cache directory. If not provided, it defaults to appname. See appdirs‘s documentation for more information.

Parameters:
  • cachename (string) – a unique name for the cache.
  • appname (string) – the name of the application the cache is created for.
  • appauthor (string or None) – the name of the application’s author – if None, defaults to appname.
cachename

The cache’s name, as passed to the Cache constructor method. This attribute’s value should not be changed unless you have good reason to do so.

cachedir

The cache’s parent directory, as determined by appdirs.user_cache_dir(). This attribute’s value should not be changed unless you have good reason to do so.

filename

The cache’s filename. It’s formed by passing cachename to hashlib‘s sha1() constructor. This attribute’s value should not be changed unless you have good reason to do so. Editing it, then getting/setting data will most likely result in errors or lost data.

set(key, value[, timeout=None])[source]

Store data in the cache. The data, value, is stored under the name, key, in the cache. The data must be picklable. Optionally, the data can expire after timeout seconds have passed.

Parameters:
  • key (string) – the name given to the data.
  • value – the data to be stored.
  • timeout (int, long, float, or None) – how long in seconds the data should be considered valid – if None, defaults to forever.
Raises:
get(key[, override=False])[source]

Get data from the cache. All data stored under the name, key, is returned. If the data is expired, None is returned. Expired data is returned if override is True.

Parameters:
  • key (string) – the name of the data to fetch.
  • override (bool) – return expired data; defaults to False.
Returns:

the requested data or None if the requested data has expired.

Raises:
invalidate(key)

Force data to expire. After forcing key to expire, calling get() on key will return None.

Parameters:

key (string) – the name of the data to invalidate.

Raises:
remove(key)[source]

Remove data from the cache. All data stored under key is deleted from the cache.

Parameters:

key (string) – the name of the data to remove.

Raises:
flush()[source]

Clear all data from the cache. This removes all key/value pairs from the cache.

Raises exceptions.IOError:
 the cache file does not exist.
delete()[source]

Delete the cache file.

On Windows, if the file is in use by another application, an exception is raised. See os.remove() for more information.

Raises exceptions.OSError:
 the cache file does not exist.

Previous topic

Usage

Next topic

Change Log

This Page