RedisCache¶
-
class
privex.helpers.cache.RedisCache.
RedisCache
(use_pickle: bool = None, redis_instance: redis.client.Redis = None, *args, **kwargs)[source]¶ A Redis backed implementation of
CacheAdapter
. Uses the global Redis instance fromprivex.helpers.plugin
by default, however custom Redis instances can be passed in via the constructor argumentredis_instance
.To allow for a wide variety of Python objects to be safely stored and retrieved from Redis, this class uses the
pickle
module for serialising + un-serialising values to/from Redis.Basic Usage:
>>> from privex.helpers import RedisCache >>> rc = RedisCache() >>> 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 Redis 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 RedisCache (not globally):>>> rc = RedisCache(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 RedisCache:>>> RedisCache.pickle_default = False
-
__init__
(use_pickle: bool = None, redis_instance: redis.client.Redis = None, *args, **kwargs)[source] RedisCache by default uses the global Redis instance from
privex.helpers.plugin
.It’s recommended to use
privex.helpers.plugin.configure_redis()
if you need to change any Redis settings, as this will adjust the global settings and re-instantiate the global instance if required.Alternatively, you may pass an instance of
redis.Redis
asredis_instance
, then that will be used instead of the global instance fromget_redis()
- Parameters
use_pickle (bool) – (Default:
True
) Use the built-inpickle
to serialise values before storing in Redis, and un-serialise when loading from Redisredis_instance (redis.Redis) – If this isn’t
None
/False
, then this Redis instance will be used instead of the global one fromget_redis()
-
Methods¶
Methods
|
RedisCache by default uses the global Redis instance from |
|
Return the value of cache key |
|
Attempt to return the value of |
|
Remove one or more keys from the cache. |
|
Set the cache key |
|
Update the timeout for a given |