soco.cache module
This module contains the classes underlying SoCo’s caching system.
- class soco.cache.NullCache(*args, **kwargs)[source]
A cache which does nothing.
Useful for debugging.
- class soco.cache.TimedCache(default_timeout=0)[source]
A simple thread-safe cache for caching method return values.
The cache key is generated by from the given
*args
and**kwargs
. Items are expired from the cache after a given period of time.Example
>>> from time import sleep >>> cache = TimedCache() >>> cache.put("item", 'some', kw='args', timeout=3) >>> # Fetch the item again, by providing the same args and kwargs. >>> assert cache.get('some', kw='args') == "item" >>> # Providing different args or kwargs will not return the item. >>> assert not cache.get('some', 'otherargs') == "item" >>> # Waiting for less than the provided timeout does not cause the >>> # item to expire. >>> sleep(2) >>> assert cache.get('some', kw='args') == "item" >>> # But waiting for longer does. >>> sleep(2) >>> assert not cache.get('some', kw='args') == "item"
Warning
At present, the cache can theoretically grow and grow, since entries are not automatically purged, though in practice this is unlikely since there are not that many different combinations of arguments in the places where it is used in SoCo, so not that many different cache entries will be created. If this becomes a problem, use a thread and timer to purge the cache, or rewrite this to use LRU logic!
- Parameters
default_timeout (int) – The default number of seconds after
expired. (which items will be) –
- put(item, *args, **kwargs)[source]
Put an item into the cache, for this combination of args and kwargs.
- Parameters
*args – any arguments.
**kwargs – any keyword arguments. If
timeout
is specified as one of the keyword arguments, the item will remain available for retrieval fortimeout
seconds. Iftimeout
isNone
or not specified, thedefault_timeout
for this cache will be used. Specify atimeout
of 0 (or ensure that thedefault_timeout
for this cache is 0) if this item is not to be cached.
- delete(*args, **kwargs)[source]
Delete an item from the cache for this combination of args and kwargs.
- class soco.cache.Cache(*args, **kwargs)[source]
A factory class which returns an instance of a cache subclass.
A
TimedCache
is returned, unlessconfig.CACHE_ENABLED
isFalse
, in which case aNullCache
will be returned.- clear()
Empty the whole cache.
- delete(*args, **kwargs)
Delete an item from the cache.
- get(*args, **kwargs)
Get an item from the cache.
- put(item, *args, **kwargs)
Put an item into the cache.