yamicache package

Submodules

yamicache.yamicache module

yamicache : Yet another in-memory cache module (‘yami’ sounds better to me than ‘yaim’)

This module provides a simple in-memory interface for caching results from function calls.

class yamicache.yamicache.Cache(hashing=True, key_join='|', debug=False, prefix=None, quiet=False, default_timeout=0, gc_thread_wait=None)[source]

Bases: _abcoll.MutableMapping

A class for caching and retreiving returns from function calls.

Parameters:
  • hashing (bool) – Whether or not to hash the function inputs when calculating the key. This helps keep the keys readable, especially for functions with many inputs.
  • key_join (str) – The character used to join the different parts that make up the hash key.
  • debug (bool) – When True, Cache.counters will be enabled and cache hits will produce output on stdout.
  • prefix (str) – All cache keys will use this prefix. Since the current implementation is instance-based, this is only helpful if dumping or comparing the cache to another instance.
  • quiet (bool) – Don’t print during debug cache hits
  • default_timeout (int) – If > 0, all cached items will be considered stale this many seconds after they are cached. In that case, the function will be run again, cached, and a new timeout value will be created.
  • gc_thread_wait (int) – The number of seconds in between cache garbage collection. The default, None, will disable the garbage collection thread. This parameter is only valid if default_timeout is > 0 (ValueError is raised otherwise).
cached(key=None, timeout=None)[source]

A decorator used to memoize the return of a function call.

clear()[source]

Clear the cache

clear_cache()[source]

A decorator used to clear the cache everytime the function is called.

For example, let’s say you have a “discovery” function that stores data read by other functions, and those function use caching. You want to use @c.clear_cache() for your main function so you don’t have to worry about cache being stale.

collect(since=None)[source]

Clear any item from the cache that has timed out.

deserialize(filename)[source]

Read the serialized cache data from a file.

dump()[source]

Dump the entire cache as a JSON string

items()[source]

Return all items in the cache as a list of tuple(key, value)

keys()[source]

Return a list of keys in the cache

pop(key)[source]

Remove the cached value specified by key

popitem()[source]

Remove a random item from the cache (only useful during testing)

serialize(filename)[source]

Serialize the cache to a filename. This process uses pickle; Do not use this function if you are caching something that is not picklable!

values()[source]

Return a list of cached values

yamicache.yamicache.nocache(*args, **kwds)[source]

Use this context manager to temporarily disable all caching for an object.

Example:

>>> from yamicache import Cache, nocache
>>> c = Cache()
>>> @c.cached
... def test():
...     return 4
...
>>> with nocache(c):
...     test()
...
4
>>> print c.data_store
{}
>>>
yamicache.yamicache.override_timeout(*args, **kwds)[source]

Module contents

Top-level package for yamicache.