Redis Backend

class cachelib.redis.RedisCache(host='localhost', port=6379, password=None, db=0, default_timeout=300, key_prefix=None, ignore_delete_many_errors=True, check_connection=False, **kwargs)

Bases: BaseRedisCache

Uses the Redis key-value store as a cache backend.

The first argument can be either a string denoting address of the Redis server or an object resembling an instance of a redis.Redis class.

Note: Python Redis API already takes care of encoding unicode strings on the fly.

Parameters:
  • host (Any) – address of the Redis server or an object which API is compatible with the official Python Redis client (redis-py).

  • port (int) – port number on which Redis server listens for connections.

  • password (str | None) – password authentication for the Redis server.

  • db (int) – db (zero-based numeric index) on Redis Server to connect.

  • default_timeout (int | timedelta) –

    the default timeout that is used if no timeout is specified on set(). Either a number of seconds or a datetime.timedelta. A timeout of 0 indicates that the cache never expires.

    Changed in version 0.17.0: Accepts a datetime.timedelta.

  • key_prefix (str | Callable[[], str] | None) – A prefix that should be added to all keys.

  • ignore_delete_many_errors (bool) –

    If False, delete_many() will raise a RuntimeError if any key fails to delete. Keys that do not exist are considered successfully deleted and do not raise.

    Changelog

    Added in version 0.16.0.

  • check_connection (bool) –

    If True, the constructor will verify the connection to the Redis server and raise a RuntimeError if it fails. If False (default), connection errors are ignored at construction and surface on first use.

    Changelog

    Added in version 0.16.1.

  • kwargs (Any)

Any additional keyword arguments will be passed to redis.Redis.

serializer = <cachelib.serializers.RedisSerializer object>
class cachelib.redis.RedisSentinelCache(sentinels, master='mymaster', password=None, db=0, default_timeout=300, key_prefix='', ignore_delete_many_errors=False, check_connection=False, **kwargs)

Bases: BaseRedisCache

Uses the Redis key-value store as a cache backend, via Redis Sentinel.

The first argument can be either a list or a tuple of sentinel addresses used to construct a redis.sentinel.Sentinel, or an object resembling an already instantiated redis.sentinel.Sentinel.

Note: Python Redis API already takes care of encoding unicode strings on the fly.

Parameters:
  • sentinels (Any) – A list or a tuple of Redis sentinel addresses, e.g. [("sentinel-host", 26379)], or an object which API is compatible with redis.sentinel.Sentinel. Required.

  • master (str) – The name of the master server in a sentinel configuration.

  • password (str | None) – password authentication for the Redis server.

  • db (int) – db (zero-based numeric index) on Redis Server to connect.

  • default_timeout (int) – the default timeout that is used if no timeout is specified on set(). A timeout of 0 indicates that the cache never expires.

  • key_prefix (str) – A prefix that should be added to all keys.

  • ignore_delete_many_errors (bool) – If set to False the delete_many method raises a RuntimeError in case a key couldn’t be deleted. Defaults to False.

  • check_connection (bool) – If True, the constructor will verify the connection to the Redis server and raise a RuntimeError if it fails.

  • kwargs (Any)

Any additional keyword arguments will be passed to redis.sentinel.Sentinel.

serializer = <cachelib.serializers.RedisSerializer object>
class cachelib.redis.RedisClusterCache(cluster='', redis_url=None, password='', default_timeout=300, key_prefix='', ignore_delete_many_errors=False, check_connection=False, **kwargs)

Bases: BaseRedisCache

Uses the Redis key-value store as a cache backend.

The first argument can be either a string denoting address of the Redis server or an object resembling an instance of a rediscluster.RedisCluster class.

Note: Python Redis API already takes care of encoding unicode strings on the fly.

Parameters:
  • cluster (Any) – The redis cluster nodes address separated by comma. e.g. host1:port1,host2:port2,host3:port3 .

  • redis_url (str | None) – A Redis URL to connect to the cluster, e.g. redis://host:port.

  • password (str)

  • default_timeout (int)

  • key_prefix (str)

  • ignore_delete_many_errors (bool)

  • check_connection (bool)

  • kwargs (Any)

One of cluster or redis_url is required. If both are supplied, redis_url wins and cluster is ignored.

Parameters:
  • password (str) – password authentication for the Redis server.

  • default_timeout (int) – the default timeout that is used if no timeout is specified on set(). A timeout of 0 indicates that the cache never expires.

  • key_prefix (str) – A prefix that should be added to all keys.

  • ignore_delete_many_errors (bool) – If set to False the delete_many method raises a RuntimeError in case a key couldn’t be deleted. Defaults to False.

  • check_connection (bool) – If True, the constructor will verify the connection to the Redis server and raise a RuntimeError if it fails.

  • cluster (Any)

  • redis_url (str | None)

  • kwargs (Any)

Any additional keyword arguments will be passed to rediscluster.RedisCluster.

serializer = <cachelib.serializers.RedisSerializer object>