CacheWrapper

class privex.helpers.cache.CacheWrapper[source]

CacheWrapper is a small class designed to wrap an instance of CacheAdapter and allow the adapter to be switched out at any time, using the static class attribute cache_instance.

This class is used for the singleton global variable cached

For convenience, if cache_instance isn’t set-up when something makes an adapter-dependant call, then the adapter class in default_adapter will be instantiated and stored in cache_instance

>>> # Using the ``: CacheAdapter`` type hinting will allow most IDEs to treat the wrapper as if it were
>>> # a normal CacheAdapter child class, thus showing appropriate completion / usage warnings
>>> c: CacheAdapter = CacheWrapper()
>>> c.set('hello', 'world')
>>> c['hello']
'world'

You can replace the cache adapter singleton using the module function adapter_set() (recommended)

>>> from privex.helpers import cache, CacheWrapper
>>> cache.adapter_set(cache.MemoryCache())   # Set the current adapter for both the cache module, and wrapper.

If you only plan to use this wrapper, then you can use set_adapter() to update the current cache adapter instance.

>>> CacheWrapper.set_adapter(cache.MemoryCache())  # Set the adapter only for the wrapper (aka ``cached``)
__init__()

Initialize self. See help(type(self)) for accurate signature.

Methods

Methods

get_adapter([default])

Attempt to get the singleton cache adapter from cache_instance - if the instance is None, then attempt to instantiate default()

set_adapter(adapter, *args, **kwargs)

Attributes

Attributes

cache_instance

Holds the singleton instance of a CacheAdapter implementation