Python Redis Dict

Python Redis Dict module.

class redis_dict.python_dict.PythonRedisDict(namespace='main', expire=None, preserve_expiration=False, redis=None, **redis_kwargs)[source]

Bases: RedisDict

Python dictionary with Redis as backend.

With support for advanced features, such as custom data types, pipelining, and key expiration.

This class focuses on having one-to-on behavior of a dictionary while using Redis as storage layer, allowing for efficient storage and retrieval of key-value pairs. It supports various data types, including strings, integers, floats, lists, dictionaries, tuples, sets, and user-defined types. The class leverages the power of Redis pipelining to minimize network round-trip time, latency, and I/O load, thereby optimizing performance for batch operations. Additionally, it allows for the management of key expiration through the use of context managers.

The RedisDict class is designed to be analogous to a standard Python dictionary while providing enhanced functionality, such as support for a wider range of data types and efficient batch operations. It aims to offer a seamless and familiar interface for developers familiar with Python dictionaries, enabling a smooth transition to a Redis-backed data store.

Extendable Types: You can extend RedisDict by adding or overriding encoding and decoding functions. This functionality enables various use cases, such as managing encrypted data in Redis, To implement this, simply create and register your custom encoding and decoding functions. By delegating serialization to redis-dict, reduce complexity and have simple code in the codebase.

__init__(namespace='main', expire=None, preserve_expiration=False, redis=None, **redis_kwargs)[source]

Initialize a RedisDict instance.

Init the RedisDict instance.

Parameters:
  • namespace (str) – A prefix for keys stored in Redis.

  • expire (Union[int, timedelta, None], optional) – Expiration time for keys.

  • preserve_expiration (Optional[bool], optional) – Preserve expiration on key updates.

  • redis (Optional[StrictRedis[Any]], optional) – A Redis connection instance.

  • **redis_kwargs (Any) – Additional kwargs for Redis connection if not provided.

clear()[source]

Remove all key-value pairs from the RedisDict in one batch operation using pipelining.

This method mimics the behavior of the clear method from a standard Python dictionary. Redis pipelining is employed to group multiple commands into a single request, minimizing network round-trip time, latency, and I/O load, thereby enhancing the overall performance.

Return type:

None

multi_chain_get(_keys)[source]

Not part of Python Redis Dict.

Parameters:

_keys (List[str]) – Not used.

Raises:

NotImplementedError – Not part of Python Redis Dict.

Return type:

List[Any]

multi_del(_key)[source]

Not part of Python Redis Dict.

Parameters:

_key (str) – Not used.

Raises:

NotImplementedError – Not part of Python Redis Dict.

Return type:

int

multi_dict(_key)[source]

Not part of Python Redis Dict.

Parameters:

_key (str) – Not used.

Raises:

NotImplementedError – Not part of Python Redis Dict.

Return type:

Dict[str, Any]

multi_get(_key)[source]

Not part of Python Redis Dict.

Parameters:

_key (str) – Not used.

Raises:

NotImplementedError – Not part of Python Redis Dict.

Return type:

List[Any]

popitem()[source]

Remove and return a random (key, value) pair from the RedisDict as a tuple.

This method is analogous to the popitem method of a standard Python dictionary.

if dict_compliant set true stays true to In Python 3.7+, removes the last inserted item (LIFO order)

Returns:

A tuple containing a randomly chosen (key, value) pair.

Return type:

tuple

Raises:

KeyError – If RedisDict is empty.

setdefault(key, default_value=None)[source]

Get value under key, and if not present set default value.

Return the value associated with the given key if it exists, otherwise set the value to the default value and return it. Analogous to a dictionary’s setdefault method.

Parameters:
  • key (str) – The key to retrieve the value.

  • default_value (Optional[Any], optional) – The value to set if the key is not found.

Returns:

The value associated with the key or the default value.

Return type:

Any