MemoryCache

class privex.helpers.cache.MemoryCache.MemoryCache(*args, enter_reconnect: Optional[bool] = None, exit_close: Optional[bool] = None, **kwargs)[source]

A very basic cache adapter which implements CacheAdapter - 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 = MemoryCache()
>>> c.set('test:example', 'hello world', timeout=60)
>>> c.get('test:example')
'hello world'
>>> sleep(60)
>>> 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.

Methods

Methods

get(key[, default, fail])

Return the value of cache key key.

get_or_set(key, value[, timeout])

Attempt to return the value of key in the cache.

remove(*key)

Remove one or more keys from the cache.

set(key, value[, timeout])

Set the cache key key to the value value, and automatically expire the key after timeout seconds from now.

update_timeout(key[, timeout])

Update the timeout for a given key to datetime.utcnow() + timedelta(seconds=timeout)