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: bool = True, key_join: str = '|', debug: bool = False, prefix: Optional[str] = None, quiet: bool = False, default_timeout: int = 0, gc_thread_wait: bool = False)[source]¶ Bases:
collections.abc.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 onstdout
. - 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 ifdefault_timeout
is > 0 (ValueError
is raised otherwise).
-
cached
(key: str = '', timeout: Optional[int] = 0)[source]¶ A decorator used to memoize the return of a function call.
-
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: Optional[float] = None) → None[source]¶ Clear any item from the cache that has timed out.
-
items
() → ItemsView[KT, VT_co][source]¶ Return all items in the cache as a list of
tuple(key, value)
-
yamicache.yamicache.
nocache
(cache_obj: yamicache.yamicache.Cache)[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 {} >>>
Module contents¶
Top-level package for yamicache.