AsyncMemoryCache¶
-
class
privex.helpers.cache.asyncx.AsyncMemoryCache.
AsyncMemoryCache
(*args, enter_reconnect: Optional[bool] = None, exit_close: Optional[bool] = None, **kwargs)[source]¶ A very basic cache adapter which implements
AsyncCacheAdapter
- stores the cache in memory using the static attribute__CACHE
As the cache is simply stored in memory, any python object can be cached without needing any form of serialization.
Fully supports cache expiration.
Basic Usage:
>>> from time import sleep >>> c = AsyncMemoryCache() >>> await c.set('test:example', 'hello world', timeout=60) >>> await c.get('test:example') 'hello world' >>> sleep(60) >>> await c.get('test:example', 'NOT FOUND') 'NOT FOUND'
-
__init__
(*args, enter_reconnect: Optional[bool] = None, exit_close: Optional[bool] = None, **kwargs) Initialize self. See help(type(self)) for accurate signature.
-
async
get
(key: str, default: Any = None, fail: bool = False) → Any[source]¶ Return the value of cache key
key
. If the key wasn’t found, or it was expired, thendefault
will be returned.Optionally, you may choose to pass
fail=True
, which will cause this method to raiseCacheNotFound
instead of returningdefault
when a key is non-existent / expired.- Parameters
key (str) – The cache key (as a string) to get the value for, e.g.
example:test
default (Any) – If the cache key
key
isn’t found / is expired, return this value (Default:None
)fail (bool) – If set to
True
, will raiseCacheNotFound
instead of returningdefault
when a key is non-existent / expired.
- Raises
CacheNotFound – Raised when
fail=True
andkey
was not found in cache / expired.- Return Any value
The value of the cache key
key
, ordefault
if it wasn’t found.
-
async
remove
(*key: str) → bool[source]¶ Remove one or more keys from the cache.
If all cache keys existed before removal,
True
will be returned. If some didn’t exist (and thus couldn’t remove), thenFalse
will be returned.- Parameters
key (str) – The cache key(s) to remove
- Return bool removed
True
ifkey
existed and was removed- Return bool removed
False
ifkey
didn’t exist, and no action was taken.
-
async
set
(key: str, value: Any, timeout: Optional[int] = 300)[source]¶ Set the cache key
key
to the valuevalue
, and automatically expire the key aftertimeout
seconds from now.If
timeout
isNone
, then the key will never expire (unless the cache implementation loses it’s persistence, e.g. memory caches with no disk writes).
-
async
update_timeout
(key: str, timeout: int = 300) → Any[source]¶ Update the timeout for a given
key
todatetime.utcnow() + timedelta(seconds=timeout)
This method should accept keys which are already expired, allowing expired cache keys to have their timeout extended after expiry.
Example:
>>> c = CacheAdapter() >>> c.set('example', 'test', timeout=60) >>> sleep(70) >>> c.update_timeout('example', timeout=60) # Reset the timeout for ``'example'`` to ``now + 60 seconds`` >>> c.get('example') 'test'
- Parameters
- Raises
CacheNotFound – Raised when
key
was not found in cache (thus cannot extend timeout)- Return Any value
The value of the cache key
-