Cache Manager#
The manager module provides the CacheManager
class.
- class cacheout.manager.CacheManager(settings: dict | None = None, cache_class: ~typing.Type[~cacheout.cache.Cache] = <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) orconfigure()
(individual configuration) methods.Example
>>> # Configure bulk caches during initialization >>> cacheset = CacheManager({"A": {"maxsize": 100}, "B": {"ttl": 90}}) >>> assert "A" in cacheset >>> assert "B" in cacheset
>>> # Replace bulk caches after initialization >>> class MyCache(Cache): pass >>> cacheset.setup({"C": {"cache_class": MyCache}, "D": {}}) >>> assert "A" not in cacheset >>> assert "B" not in cacheset >>> assert "C" in cacheset >>> assert isinstance(cacheset["C"], MyCache) >>> assert "D" in cacheset >>> assert isinstance(cacheset["D"], Cache)
>>> # Configure individual cache after initialization >>> cacheset.configure("E", **{"cache_class": MyCache}) >>> assert isinstance(cacheset["E"], MyCache)
>>> # Replace a cache entity >>> cacheset.register("E", Cache()) >>> assert isinstance(cacheset["E"], Cache)
>>> # Access caches >>> cacheset["C"].set("key1", "value1") >>> cacheset["D"].set("key2", "value2")
- Parameters:
settings (dict, optional) – A
dict
indexed by each cache name that should be created and configured. Defaults toNone
which doesn’t configure anything.cache_class (callable, optional) – A factory function used when creating a cache.
- configure(name: Hashable, **options: Any) None [source]#
Configure cache identified by name.
Note
If no cache has been configured for name, then it will be created.
- Parameters:
name – Cache name identifier.
**options – Cache options.
- setup(settings: dict | None = None) 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.