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
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ 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"',
]

[project.optional-dependencies]
hiredis = [
"hiredis>=3.2.0",
]

xxhash = [
'xxhash~=3.6.0',
]

ocsp = [
"cryptography>=36.0.1",
"pyopenssl>=20.0.1",
Expand Down
44 changes: 43 additions & 1 deletion redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

import datetime
import hashlib

# Try to import the xxhash library as an optional dependency
try:
import xxhash
HAS_XXHASH = True
except ImportError:
HAS_XXHASH = False
from redis.xxh3 import xxh3_64_hexdigest

import warnings
from enum import Enum
from typing import (
Expand Down Expand Up @@ -1889,7 +1898,40 @@ def expiretime(self, key: str) -> int:
return self.execute_command("EXPIRETIME", key)

@experimental_method()
def digest(self, name: KeyT) -> Optional[str]:
def digest_local(self, value: Union[bytes, str]) -> Union[bytes, 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
"""
if HAS_XXHASH:
local_digest = xxhash.xxh3_64(value).hexdigest()
else:
local_digest = xxh3_64_hexdigest(value)

# To align with digest, we want to return bytes if decode_responses is False.
# The following should work because Python's mixin approach.
if hasattr(self, 'connection_pool'):
if not self.connection_pool.connection_kwargs.get('decode_responses', False):
local_digest = local_digest.encode()

return local_digest


@experimental_method()
def digest(self, name: KeyT) -> Union[str, bytes, None]:
"""
Return the digest of the value stored at the specified key.

Expand Down
Loading
Loading