Caching to a local disk

Persistent memoisation backend that saves results in the local file system

The persist decorator takes a cache argument, which details what sort of backend to use for the cache. If this string begins with “file://”, or if no cache is specified, then a disk cache is used, which saves computed results to a directory in the local file system. This internal work is done by the classes defined below.

class pypersist.diskcache.Cache(func, dir)[source]

Dictionary-like object for saving function outputs to disk

This cache, which can be used by the persist decorator in persist.py, stores computed values on disk in a specified directory so that they can be restored later using a key. Like a dictionary, a key-value pair can be added using cache[key] = val, looked up using cache[key], and removed using del cache[key]. The number of values stored can be found using len(cache).

A disk cache might not store its keys, and therefore we cannot iterate through its keys as we can with a dictionary. However, see CacheWithKeys.

Parameters:
  • func (persist_wrapper) – Memoised function whose results this is caching. Options which are not specific to local disk storage, such as the key, hash, and pickle functions, are taken from this.
  • dir (str) – Directory into which to save results. The same directory can be used for several different functions, since a subdirectory will be created for each function based on its funcname.
clear()[source]

Delete all the results stored in this cache

class pypersist.diskcache.CacheWithKeys(func, dir)[source]

Mutable mapping for saving function outputs to disk

This subclass of Cache can be used in place of Cache whenever storekey is True or unhash is set, to implement the MutableMapping abstract base class. This allows the cache to be used exactly like a dictionary, including the ability to iterate through all keys in the cache.

class KeysIter(cache)[source]

Iterator class for the keys of a CacheWithKeys object

next()

Return the next item from the iterator. When exhausted, raise StopIteration