Cache Manager

The manager module provides the CacheManager class.

class cacheout.manager.CacheManager(settings=None, cache_class=<class 'cacheout.cache.Cache'>)[source]

The cache manager provides an interface for accessing multiple caches indexed by name.

Each named cache is a separate cache instance with its own configuration. Named caches can be configured during initialization or later using the setup() (bulk configuration) or configure() (individual configuration) methods.

Example

>>> # Configure bulk caches during initialization
>>> caches = CacheManager({"A": {"maxsize": 100}, "B": {"ttl": 90}})
>>> assert "A" in caches
>>> assert "B" in caches
>>> # Replace bulk caches after initializtion
>>> class MyCache(Cache): pass
>>> caches.setup({"C": {"cache_class": MyCache}, "D": {}})
>>> assert "A" not in caches
>>> assert "B" not in caches
>>> assert "C" in caches
>>> assert isinstance(caches["C"], MyCache)
>>> assert "D" in caches
>>> assert isinstance(caches["D"], Cache)
>>> # Configure individual cache after initializtaion
>>> caches.configure("E", **{"cache_class": MyCache})
>>> assert isinstance(caches["E"], MyCache)
>>> # Replace a cache entity
>>> caches.register("E", Cache())
>>> assert isinstance(caches["E"], Cache)
>>> # Access caches
>>> caches["C"].set("key1", "value1")
>>> caches["D"].set("key2", "value2")
Parameters:
  • settings (dict, optional) – A dict indexed by each cache name that should be created and configured. Defaults to None which doesn’t configure anything.
  • cache_class (callable, optional) – A factory function used when creating a cache.
cache_names()[source]

Return list of names of cache entities.

Returns:list
caches()[source]

Return list of cache instances.

Returns:list
clear_all()[source]

Clear all caches.

configure(name, **options)[source]

Configure cache identified by name.

Note

If no cache has been configured for name, then it will be created.

Keyword Arguments:
 **options – Cache options.
register(name, cache)[source]

Register a named cache instance.

setup(settings=None)[source]

Set up named cache instances using configuration defined in settings.

The settings should contain key/values corresponding to the cache name and its cache options, respectively. Named caches are then accessible using index access on the cache handler object.

Warning

Calling this method will destroy all previously configured named caches and replace them with what is defined in settings.

Parameters:settings (dict, optional) – A dict indexed by each cache name that contains the options for each named cache.