Python Valkey Dict

Python Valkey Dict module.

class valkey_dict.python_dict.PythonValkeyDict(namespace='main', expire=None, preserve_expiration=False, valkey=None, **valkey_kwargs)[source]

Bases: ValkeyDict

Python dictionary with Valkey 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 Valkey 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 Valkey 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 ValkeyDict 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 Valkey-backed data store.

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

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

Initialize a ValkeyDict instance.

Init the ValkeyDict instance.

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

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

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

  • valkey (Optional[StrictValkey[Any]], optional) – A Valkey connection instance.

  • **valkey_kwargs (Any) – Additional kwargs for Valkey connection if not provided.

clear()[source]

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

This method mimics the behavior of the clear method from a standard Python dictionary. Valkey 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 Valkey Dict.

Parameters:

_keys (List[str]) – Not used.

Raises:

NotImplementedError – Not part of Python Valkey Dict.

Return type:

List[Any]

multi_del(_key)[source]

Not part of Python Valkey Dict.

Parameters:

_key (str) – Not used.

Raises:

NotImplementedError – Not part of Python Valkey Dict.

Return type:

int

multi_dict(_key)[source]

Not part of Python Valkey Dict.

Parameters:

_key (str) – Not used.

Raises:

NotImplementedError – Not part of Python Valkey Dict.

Return type:

Dict[str, Any]

multi_get(_key)[source]

Not part of Python Valkey Dict.

Parameters:

_key (str) – Not used.

Raises:

NotImplementedError – Not part of Python Valkey Dict.

Return type:

List[Any]

popitem()[source]

Remove and return a random (key, value) pair from the ValkeyDict 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 ValkeyDict 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