Caching to a MongoDB database

Persistent memoisation backend that saves results on a MongoDB REST server

The persist decorator takes a cache argument, which details what sort of backend to use for the cache. If this string begins with “mongodb://”, then a MongoDB cache is used, which saves computed results to a MongoDB database via a REST API. This internal work is done by the classes defined below.

To start a MongoDB/REST server for use with this cache, navigate to the mongodb_server/ directory and execute the run.py script.

class pypersist.mongodbcache.Cache(func, url)[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 in a specified MongoDB database 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 MongoDB 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.
  • url (str) – URL of the pypersist MongoDB database that will be used to store and load results. The same database can be used for several different functions, since the function’s funcname will be stored with each result.
_get_db(hash=None)[source]

Return all db items for this function, or one with this hash

Queries the MongoDB database for entries with this function, and returns the resulting json data as a dictionary.

Parameters:hash (str, optional) – The hash of the database item we wish to retrieve.
Returns:If a hash is specified, a single database item with entries “_id”, “_etag”, “funcname”, “hash”, “result” and so on. If no hash is specified, a list of all such items in the database in the “_items” entry, along with metadata in the “_meta” entry. If no appropriate item exists in the database, None.
Return type:dict or None
clear()[source]

Delete all the results stored in this cache

class pypersist.mongodbcache.CacheWithKeys(func, url)[source]

Mutable mapping for saving function outputs to a MongoDB database

This subclass of Cache can be used in place of Cache whenever storekey is True or unhash is defined, 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