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 fromprivex.helpers.plugin
by default, however custom Memcached instances can be passed in via the constructor argumentmcache_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
toFalse
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
asmcache_instance
, then that will be used instead of the global instance fromget_memcached_async()
- Parameters
use_pickle (bool) – (Default:
True
) Use the built-inpickle
to serialise values before storing in Memcached, and un-serialise when loading from Memcachedmcache_instance (aiomcache.Client) – If this isn’t
None
/False
, then this Memcached instance will be used instead of the global one fromget_memcached_async()
-
Methods¶
Methods
|
AsyncMemcachedCache by default uses the global Memcached instance from |
|
Return the value of cache key |
|
Remove one or more keys from the cache. |
|
Set the cache key |
|
Update the timeout for a given |