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, then default will be returned.

Optionally, you may choose to pass fail=True, which will cause this method to raise CacheNotFound instead of returning default 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 raise CacheNotFound instead of returning default when a key is non-existent / expired.

Raises

CacheNotFound – Raised when fail=True and key was not found in cache / expired.

Return Any value

The value of the cache key key, or default 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), then False will be returned.

Parameters

key (str) – The cache key(s) to remove

Return bool removed

True if key existed and was removed

Return bool removed

False if key 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 value value, and automatically expire the key after timeout seconds from now.

If timeout is None, then the key will never expire (unless the cache implementation loses it’s persistence, e.g. memory caches with no disk writes).

Parameters
  • key (str) – The cache key (as a string) to set the value for, e.g. example:test

  • value (Any) – The value to store in the cache key key

  • timeout (int) – The amount of seconds to keep the data in cache. Pass None to disable expiration.

async update_timeout(key: str, timeout: int = 300) → Any[source]

Update the timeout for a given key to datetime.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
  • key (str) – The cache key to update the timeout for

  • timeout (int) – Reset the timeout to this many seconds from datetime.utcnow()

Raises

CacheNotFound – Raised when key was not found in cache (thus cannot extend timeout)

Return Any value

The value of the cache key