AsyncCacheWrapper¶
-
class
privex.helpers.cache.AsyncCacheWrapper[source]¶ For applications/packages which are primarily AsyncIO,
CacheWrappercan cause problems such as the commonevent loop is already running- and unfortunately,nest_asyncioisn’t always able to fix it.This wrapper -
AsyncCacheWrapperis ONLY compatible with AsyncIO code, and holds a different adapter instance incache_instancethanCacheWrapper, as it expects the adapter instance to always be based onAsyncCacheAdapter- 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
AsyncCacheAdapterimplementation
-
default_adapter: Union[Type[privex.helpers.cache.asyncx.base.AsyncCacheAdapter], str] = 'memory'¶ The default adapter class to instantiate if
cache_instanceisNone
-
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 isNone, then attempt to instantiatedefault()If any
*argsor**kwargsare passed, they will be passed through todefault(*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_instancewith the same adapter class (assuming it’s set)
-
Methods¶
Methods
|
Attempt to get the singleton cache adapter from |
|
Re-create the adapter instance at |
|