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 from privex.helpers.plugin by default, however custom Redis instances can be passed in via the constructor argument redis_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 to False 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 as redis_instance, then that will be used instead of the global instance from get_redis()

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

  • redis_instance (redis.Redis) – If this isn’t None / False, then this Redis instance will be used instead of the global one from get_redis()

Methods

Methods

__init__([use_pickle, redis_instance])

RedisCache by default uses the global Redis instance from privex.helpers.plugin.

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)

Attributes

Attributes

pickle_default

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