MemcachedCache

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

A Memcached backed implementation of CacheAdapter. 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 MemcachedCache
>>> rc = MemcachedCache()
>>> 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 = MemcachedCache(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 MemcachedCache:

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

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

It’s recommended to use privex.helpers.plugin.configure_memcached() 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 pylibmc.Client as mcache_instance, then that will be used instead of the global instance from get_memcached()

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 (pylibmc.Client) – If this isn’t None / False, then this Memcached instance will be used instead of the global one from get_memcached()

  • enter_reconnect (bool) – Pass enter_reconnect=False to disable calling reconnect() when entering this cache adapter as a context manager (__aenter__())

  • exit_close (bool) – Pass exit_close=False to disable calling close() when exiting this cache adapter as a context manager (__aexit__())

close()[source]

Close any cache library connections, and destroy their local class instances by setting them to None.

connect(*args, new_connection=True, **kwargs) → pylibmc.client.Client[source]

Create an instance of the library used to interact with the caching system, ensure it’s connection is open, and store the instance on this class instance - only if not already connected.

Should return the class instance which was created.

get(key: Union[bytes, 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.

pickle_default: bool = True

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

remove(*key: Union[bytes, 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.

set(key: Union[bytes, 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.

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

use_pickle: bool

If True, will use pickle for serializing objects before inserting into Memcached, and un-serialising objects retrieved from Memcached. This attribute is set in __init__().

Change this to False to disable the use of pickle - instead values will be passed to / returned from Memcached as-is, with no serialisation (this may require you to manually serialize complex types such as dict and Decimal before insertion, and un-serialise after retrieval).

Methods

Methods

__init__([use_pickle, mcache_instance])

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

close()

Close any cache library connections, and destroy their local class instances by setting them to None.

connect(*args[, new_connection])

Create an instance of the library used to interact with the caching system, ensure it’s connection is open, and store the instance on this class instance - only if not already connected.

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

adapter_enter_reconnect

adapter_exit_close

mcache

pickle_default

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