AsyncCacheWrapper

class privex.helpers.cache.AsyncCacheWrapper[source]

For applications/packages which are primarily AsyncIO, CacheWrapper can cause problems such as the common event loop is already running - and unfortunately, nest_asyncio isn’t always able to fix it.

This wrapper - AsyncCacheWrapper is ONLY compatible with AsyncIO code, and holds a different adapter instance in cache_instance than CacheWrapper, as it expects the adapter instance to always be based on AsyncCacheAdapter - along with the fact it generally returns coroutines.

>>> from privex.helpers.cache import async_cached, async_adapter_set, AsyncRedisCache
>>> async_adapter_set(AsyncRedisCache())
>>> c = async_cached      # We alias async_cached to 'c' for convenience.
>>> c['hello'] = 'world'  # We can set cache keys as if the wrapper was a dictionary
>>> c['hello']            # Similarly we can also get keys like normal
'world'
>>> await c['hello']      # It's safest to use ``await`` when getting keys within an async context
'world'
>>> await c.get('hello')  # We can also use the coroutines .get, .set, .get_or_set etc.
'world'
__init__()

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

cache_instance: privex.helpers.cache.asyncx.base.AsyncCacheAdapter = <privex.helpers.cache.asyncx.AsyncMemoryCache.AsyncMemoryCache object>

Holds the singleton instance of a AsyncCacheAdapter implementation

default_adapter: Union[Type[privex.helpers.cache.asyncx.base.AsyncCacheAdapter], str] = 'memory'

The default adapter class to instantiate if cache_instance is None

classmethod get_adapter(default: Union[Type[privex.helpers.cache.asyncx.base.AsyncCacheAdapter], str] = 'memory', *args, **kwargs)privex.helpers.cache.asyncx.base.AsyncCacheAdapter[source]

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

If any *args or **kwargs are passed, they will be passed through to default(*args, **kwargs) so that any necessary configuration parameters can be passed to the class.

classmethod reset_adapter(default: Type[privex.helpers.cache.asyncx.base.AsyncCacheAdapter] = 'memory', *args, **kwargs)privex.helpers.cache.asyncx.base.AsyncCacheAdapter[source]

Re-create the adapter instance at cache_instance with the same adapter class (assuming it’s set)

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

reset_adapter([default])

Re-create the adapter instance at cache_instance with the same adapter class (assuming it’s set)

set_adapter(adapter, *args, **kwargs)

Attributes

Attributes

cache_instance

Holds the singleton instance of a AsyncCacheAdapter implementation

instance_args

instance_kwargs

max_context_layers