Redis Backend¶
- class cachelib.redis.RedisCache(host='localhost', port=6379, password=None, db=0, default_timeout=300, key_prefix=None, **kwargs)¶
Bases:
cachelib.base.BaseCache
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 (Optional[str]) – 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 (Optional[str]) – A prefix that should be added to all keys.
kwargs (Any) –
Any additional keyword arguments will be passed to
redis.Redis
.- add(key, value, timeout=None)¶
Works like
set()
but does not overwrite the values of already existing keys.- Parameters
key (str) – the key to set
value (Any) – the value for the key
timeout (Optional[int]) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.
- Returns
Same as
set()
, but alsoFalse
for already existing keys.- Return type
boolean
- clear()¶
Clears the cache. Keep in mind that not all caches support completely clearing the cache.
- Returns
Whether the cache has been cleared.
- Return type
boolean
- dec(key, delta=1)¶
Decrements the value of a key by delta. If the key does not yet exist it is initialized with -delta.
For supporting caches this is an atomic operation.
- Parameters
key (str) – the key to increment.
delta (int) – the delta to subtract.
- Returns
The new value or None for backend errors.
- Return type
Optional[int]
- delete(key)¶
Delete key from the cache.
- Parameters
key (str) – the key to delete.
- Returns
Whether the key existed and has been deleted.
- Return type
boolean
- delete_many(*keys)¶
Deletes multiple keys at once.
- Parameters
keys (str) – The function accepts multiple keys as positional arguments.
- Returns
A list containing all sucessfuly deleted keys
- Return type
boolean
- dump_object(value)¶
Dumps an object into a string for redis. By default it serializes integers as regular string and pickle dumps everything else.
- Parameters
value (Any) –
- Return type
bytes
- get(key)¶
Look up key in the cache and return the value for it.
- Parameters
key (str) – the key to be looked up.
- Returns
The value if it exists and is readable, else
None
.- Return type
Any
- get_many(*keys)¶
Returns a list of values for the given keys. For each key an item in the list is created:
foo, bar = cache.get_many("foo", "bar")
Has the same error handling as
get()
.- Parameters
keys (str) – The function accepts multiple keys as positional arguments.
- Return type
List[Any]
- has(key)¶
Checks if a key exists in the cache without returning it. This is a cheap operation that bypasses loading the actual data on the backend.
- Parameters
key (str) – the key to check
- Return type
bool
- inc(key, delta=1)¶
Increments the value of a key by delta. If the key does not yet exist it is initialized with delta.
For supporting caches this is an atomic operation.
- Parameters
key (str) – the key to increment.
delta (int) – the delta to add.
- Returns
The new value or
None
for backend errors.- Return type
Optional[int]
- load_object(value)¶
The reversal of
dump_object()
. This might be called with None.- Parameters
value (Optional[bytes]) –
- Return type
Any
- set(key, value, timeout=None)¶
Add a new key/value to the cache (overwrites value, if key already exists in the cache).
- Parameters
key (str) – the key to set
value (Any) – the value for the key
timeout (Optional[int]) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.
- Returns
True
if key has been updated,False
for backend errors. Pickling errors, however, will raise a subclass ofpickle.PickleError
.- Return type
boolean
- set_many(mapping, timeout=None)¶
Sets multiple keys and values from a mapping.
- Parameters
mapping (Dict[str, Any]) – a mapping with the keys/values to set.
timeout (Optional[int]) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.
- Returns
A list containing all keys sucessfuly set
- Return type
boolean