AsyncMemcachedCache

class privex.helpers.cache.asyncx.AsyncMemcachedCache.AsyncMemcachedCache(use_pickle: bool = None, mcache_instance: aiomcache.client.Client = None, *args, **kwargs)[source]

A Memcached backed implementation of AsyncCacheAdapter. Uses the global Memcached instance from privex.helpers.plugin by default, however custom Memcached instances can be passed in via the constructor argument mcache_instance.

To allow for a wide variety of Python objects to be safely stored and retrieved from Memcached, this class uses the pickle module for serialising + un-serialising values to/from Memcached.

Basic Usage:

>>> from privex.helpers import AsyncMemcachedCache
>>> rc = AsyncMemcachedCache()
>>> await rc.set('hello', 'world')
>>> rc['hello']
'world'

Disabling Pickling

In some cases, you may need interoperable caching with other languages. The pickle serialisation technique is extremely specific to Python and is largely unsupported outside of Python. Thus if you need to share Memcached cache data with applications in other languages, then you must disable pickling.

WARNING: If you disable pickling, then you must perform your own serialisation + de-serialization on complex objects such as dict, list, Decimal, or arbitrary classes/functions after getting or setting cache keys.

Disabling Pickle per instance

Pass use_pickle=False to the constructor, or access the attribute directly to disable pickling for a single instance of MemcachedCache (not globally):

>>> rc = AsyncMemcachedCache(use_pickle=False)  # Opt 1. Disable pickle in constructor
>>> rc.use_pickle = False                       # Opt 2. Disable pickle on an existing instance

Disabling Pickle by default on any new instances

Change the static attribute pickle_default to False to disable the use of pickle by default across any new instances of AsyncMemcachedCache:

>>> AsyncMemcachedCache.pickle_default = False
__init__(use_pickle: bool = None, mcache_instance: aiomcache.client.Client = None, *args, **kwargs)[source]

AsyncMemcachedCache by default uses the global Memcached instance from privex.helpers.plugin.

It’s recommended to use privex.helpers.plugin.configure_memcached_async() if you need to change any Memcached settings, as this will adjust the global settings and re-instantiate the global instance if required.

Alternatively, you may pass an instance of aiomcache.Client as mcache_instance, then that will be used instead of the global instance from get_memcached_async()

Parameters
  • use_pickle (bool) – (Default: True) Use the built-in pickle to serialise values before storing in Memcached, and un-serialise when loading from Memcached

  • mcache_instance (aiomcache.Client) – If this isn’t None / False, then this Memcached instance will be used instead of the global one from get_memcached_async()

Methods

Methods

__init__([use_pickle, mcache_instance])

AsyncMemcachedCache by default uses the global Memcached instance from privex.helpers.plugin.

get(key[, default, fail])

Return the value of cache key key.

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)

Attributes

Attributes

mcache

pickle_default

Change this to False to disable the use of pickle by default for any new instances of this class.