Skip to content

Commit 360860e

Browse files
[#3612] Generating unique command cache key (#3765)
* Fix for _current_command_cache_key generating same against key for different fields * correction in test_read_response_returns_cached_reply test case for new redis_args field * Update redis/cache.py * linter checks * Fixing resp3 failing tests.Adding hash fields behavior test
1 parent 65bde88 commit 360860e

File tree

4 files changed

+437
-81
lines changed

4 files changed

+437
-81
lines changed

redis/cache.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@ class EvictionPolicyType(Enum):
1717

1818
@dataclass(frozen=True)
1919
class CacheKey:
20+
"""
21+
Represents a unique key for a cache entry.
22+
23+
Attributes:
24+
command (str): The Redis command being cached.
25+
redis_keys (tuple): The Redis keys involved in the command.
26+
redis_args (tuple): Additional arguments for the Redis command.
27+
This field is included in the cache key to ensure uniqueness
28+
when commands have the same keys but different arguments.
29+
Changing this field will affect cache key uniqueness.
30+
"""
31+
2032
command: str
2133
redis_keys: tuple
34+
redis_args: tuple = () # Additional arguments for the Redis command; affects cache key uniqueness.
2235

2336

2437
class CacheEntry:

redis/connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ def send_command(self, *args, **kwargs):
14231423
with self._cache_lock:
14241424
# Command is write command or not allowed
14251425
# to be cached.
1426-
if not self._cache.is_cachable(CacheKey(command=args[0], redis_keys=())):
1426+
if not self._cache.is_cachable(
1427+
CacheKey(command=args[0], redis_keys=(), redis_args=())
1428+
):
14271429
self._current_command_cache_key = None
14281430
self._conn.send_command(*args, **kwargs)
14291431
return
@@ -1433,7 +1435,7 @@ def send_command(self, *args, **kwargs):
14331435

14341436
# Creates cache key.
14351437
self._current_command_cache_key = CacheKey(
1436-
command=args[0], redis_keys=tuple(kwargs.get("keys"))
1438+
command=args[0], redis_keys=tuple(kwargs.get("keys")), redis_args=args
14371439
)
14381440

14391441
with self._cache_lock:

0 commit comments

Comments
 (0)