Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = ['async-timeout>=4.0.3; python_full_version<"3.11.3"']
dependencies = [
'async-timeout>=4.0.3; python_full_version<"3.11.3"',
'xxhash~=3.6.0',
]

[project.optional-dependencies]
hiredis = [
Expand Down
22 changes: 22 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import hashlib
import xxhash
import warnings
from enum import Enum
from typing import (
Expand Down Expand Up @@ -1888,6 +1889,27 @@ def expiretime(self, key: str) -> int:
"""
return self.execute_command("EXPIRETIME", key)

@experimental_method()
def digest_local(self, value: Union[bytes, str]) -> str:
"""
Compute the hexadecimal digest of the value locally, without sending it to the server.

This is useful for conditional operations like IFDEQ/IFDNE where you need to
compute the digest client-side before sending a command.

Warning:
**Experimental** - This API may change or be removed without notice.

Arguments:
- value: Union[bytes, str] - the value to compute the digest of.

Returns:
- (str) the XXH3 digest of the value as a hex string (16 hex characters)

For more information, see https://redis.io/commands/digest
"""
return xxhash.xxh3_64(value).hexdigest()

@experimental_method()
def digest(self, name: KeyT) -> Optional[str]:
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,9 @@ def test_set_ifdeq_and_ifdne(self, r, val):
d = self._server_xxh3_digest(r, "k")
assert d is not None

# sanity check: local digest matches server's
assert d == r.digest_local(val)

# IFDEQ must match to set; if key missing => won't create
assert r.set("k", b"X", ifdeq=d) is True
assert r.get("k") == b"X"
Expand Down
Loading