async_cached¶
-
privex.helpers.cache.
async_cached
: Union[privex.helpers.cache.asyncx.base.AsyncCacheAdapter, privex.helpers.cache.AsyncCacheWrapper] = <privex.helpers.cache.AsyncCacheWrapper object>¶ For applications/packages which are primarily AsyncIO,
CacheWrapper
can cause problems such as the commonevent 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 incache_instance
thanCacheWrapper
, 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'