Skip to content
13 changes: 13 additions & 0 deletions redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ class EvictionPolicyType(Enum):

@dataclass(frozen=True)
class CacheKey:
"""
Represents a unique key for a cache entry.
Attributes:
command (str): The Redis command being cached.
redis_keys (tuple): The Redis keys involved in the command.
redis_args (tuple): Additional arguments for the Redis command.
This field is included in the cache key to ensure uniqueness
when commands have the same keys but different arguments.
Changing this field will affect cache key uniqueness.
"""

command: str
redis_keys: tuple
redis_args: tuple = () # Additional arguments for the Redis command; affects cache key uniqueness.


class CacheEntry:
Expand Down
6 changes: 4 additions & 2 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,9 @@ def send_command(self, *args, **kwargs):
with self._cache_lock:
# Command is write command or not allowed
# to be cached.
if not self._cache.is_cachable(CacheKey(command=args[0], redis_keys=())):
if not self._cache.is_cachable(
CacheKey(command=args[0], redis_keys=(), redis_args=())
):
self._current_command_cache_key = None
self._conn.send_command(*args, **kwargs)
return
Expand All @@ -1433,7 +1435,7 @@ def send_command(self, *args, **kwargs):

# Creates cache key.
self._current_command_cache_key = CacheKey(
command=args[0], redis_keys=tuple(kwargs.get("keys"))
command=args[0], redis_keys=tuple(kwargs.get("keys")), redis_args=args
)

with self._cache_lock:
Expand Down
Loading