soco.cache module

This module contains the classes underlying SoCo’s caching system.

class soco.cache._BaseCache(*args, **kwargs)[source]

An abstract base class for the cache.

enabled

whether the cache is enabled

Type

bool

put(item, *args, **kwargs)[source]

Put an item into the cache.

get(*args, **kwargs)[source]

Get an item from the cache.

delete(*args, **kwargs)[source]

Delete an item from the cache.

clear()[source]

Empty the whole cache.

class soco.cache.NullCache(*args, **kwargs)[source]

A cache which does nothing.

Useful for debugging.

put(item, *args, **kwargs)[source]

Put an item into the cache.

get(*args, **kwargs)[source]

Get an item from the cache.

delete(*args, **kwargs)[source]

Delete an item from the cache.

clear()[source]

Empty the whole cache.

enabled

whether the cache is enabled

Type

bool

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) –

default_timeout

The default caching expiry interval in seconds.

Type

int

get(*args, **kwargs)[source]

Get an item from the cache for this combination of args and kwargs.

Parameters
  • *args – any arguments.

  • **kwargs – any keyword arguments.

Returns

The object which has been found in the cache, or None if no unexpired item is found. This means that there is no point storing an item in the cache if it is None.

Return type

object

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 for timeout seconds. If timeout is None or not specified, the default_timeout for this cache will be used. Specify a timeout of 0 (or ensure that the default_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.

clear()[source]

Empty the whole cache.

static make_key(*args, **kwargs)[source]

Generate a unique, hashable, representation of the args and kwargs.

Parameters
  • *args – any arguments.

  • **kwargs – any keyword arguments.

Returns

the key.

Return type

str

enabled

whether the cache is enabled

Type

bool

class soco.cache.Cache(*args, **kwargs)[source]

A factory class which returns an instance of a cache subclass.

A TimedCache is returned, unless config.CACHE_ENABLED is False, in which case a NullCache 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.

enabled

whether the cache is enabled

Type

bool