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__CACHEAs 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, thendefaultwill be returned.Optionally, you may choose to pass
fail=True, which will cause this method to raiseCacheNotFoundinstead of returningdefaultwhen a key is non-existent / expired.- Parameters
key (str) – The cache key (as a string) to get the value for, e.g.
example:testdefault (Any) – If the cache key
keyisn’t found / is expired, return this value (Default:None)fail (bool) – If set to
True, will raiseCacheNotFoundinstead of returningdefaultwhen a key is non-existent / expired.
- Raises
CacheNotFound – Raised when
fail=Trueandkeywas not found in cache / expired.- Return Any value
The value of the cache key
key, ordefaultif 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,
Truewill be returned. If some didn’t exist (and thus couldn’t remove), thenFalsewill be returned.- Parameters
key (str) – The cache key(s) to remove
- Return bool removed
Trueifkeyexisted and was removed- Return bool removed
Falseifkeydidn’t exist, and no action was taken.
-
async
set(key: str, value: Any, timeout: Optional[int] = 300)[source]¶ Set the cache key
keyto the valuevalue, and automatically expire the key aftertimeoutseconds from now.If
timeoutisNone, 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
keytodatetime.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
keywas not found in cache (thus cannot extend timeout)- Return Any value
The value of the cache key
-