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
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ lint.select = [

lint.ignore = [
"COM812", # missing trailing comma, covered by black
"ANN101", # ignore missing type annotation in self parameter
"S311", # ignore Standard pseudo-random generators because they are not used for cryptographic purposes
"A004", # ignore Checks for imports that use the same names as builtins.
]

fix = true
Expand Down
1 change: 1 addition & 0 deletions changelog.d/768.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Consider the version when generating keys to work with hashmap functions
29 changes: 18 additions & 11 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ def sscan(

cursor, result = client.sscan(
key,
match=cast(PatternT, self.encode(match)) if match else None,
match=cast("PatternT", self.encode(match)) if match else None,
count=count,
)
return {self.decode(value) for value in result}
Expand All @@ -1024,7 +1024,7 @@ def sscan_iter(
key = self.make_key(key, version=version)
for value in client.sscan_iter(
key,
match=cast(PatternT, self.encode(match)) if match else None,
match=cast("PatternT", self.encode(match)) if match else None,
count=count,
):
yield self.decode(value)
Expand Down Expand Up @@ -1099,7 +1099,7 @@ def touch(

def hset(
self,
name: str,
name: KeyT,
key: KeyT,
value: EncodableT,
version: Optional[int] = None,
Expand All @@ -1111,13 +1111,14 @@ def hset(
"""
if client is None:
client = self.get_client(write=True)
key_name = self.make_key(name, version=version)
nkey = self.make_key(key, version=version)
nvalue = self.encode(value)
return int(client.hset(name, nkey, nvalue))
return int(client.hset(key_name, nkey, nvalue))

def hdel(
self,
name: str,
name: KeyT,
key: KeyT,
version: Optional[int] = None,
client: Optional[Redis] = None,
Expand All @@ -1128,39 +1129,44 @@ def hdel(
"""
if client is None:
client = self.get_client(write=True)
key_name = self.make_key(name, version=version)
nkey = self.make_key(key, version=version)
return int(client.hdel(name, nkey))
return int(client.hdel(key_name, nkey))

def hlen(
self,
name: str,
name: KeyT,
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> int:
"""
Return the number of items in hash name.
"""
if client is None:
client = self.get_client(write=False)
return int(client.hlen(name))
key_name = self.make_key(name, version=version)
return int(client.hlen(key_name))

def hkeys(
self,
name: str,
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> List[Any]:
"""
Return a list of keys in hash name.
"""
key_name = self.make_key(name, version=version)
if client is None:
client = self.get_client(write=False)
try:
return [self.reverse_key(k.decode()) for k in client.hkeys(name)]
return [self.reverse_key(k.decode()) for k in client.hkeys(key_name)]
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client) from e

def hexists(
self,
name: str,
name: KeyT,
key: KeyT,
version: Optional[int] = None,
client: Optional[Redis] = None,
Expand All @@ -1170,5 +1176,6 @@ def hexists(
"""
if client is None:
client = self.get_client(write=False)
key_name = self.make_key(name, version=version)
nkey = self.make_key(key, version=version)
return bool(client.hexists(name, nkey))
return bool(client.hexists(key_name, nkey))
6 changes: 3 additions & 3 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def test_delete_many_generator(self, cache: RedisCache):
assert bool(res) is False

def test_delete_many_empty_generator(self, cache: RedisCache):
res = cache.delete_many(key for key in cast(List[str], []))
res = cache.delete_many(key for key in cast("List[str]", []))
assert bool(res) is False

def test_incr(self, cache: RedisCache):
Expand Down Expand Up @@ -749,7 +749,7 @@ def test_primary_replica_switching(self, cache: RedisCache):
if isinstance(cache.client, ShardClient):
pytest.skip("ShardClient doesn't support get_client")

cache = cast(RedisCache, caches["sample"])
cache = cast("RedisCache", caches["sample"])
client = cache.client
client._server = ["foo", "bar"]
client._clients = ["Foo", "Bar"]
Expand All @@ -761,7 +761,7 @@ def test_primary_replica_switching_with_index(self, cache: RedisCache):
if isinstance(cache.client, ShardClient):
pytest.skip("ShardClient doesn't support get_client")

cache = cast(RedisCache, caches["sample"])
cache = cast("RedisCache", caches["sample"])
client = cache.client
client._server = ["foo", "bar"]
client._clients = ["Foo", "Bar"]
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cache_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def ignore_exceptions_cache(settings) -> RedisCache:
settings.CACHES = caches_setting
settings.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
settings.DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
return cast(RedisCache, caches["doesnotexist"])
return cast("RedisCache", caches["doesnotexist"])


def test_get_django_omit_exceptions_many_returns_default_arg(
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_get_django_omit_exceptions_priority_1(settings):
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
settings.CACHES = caches_setting
settings.DJANGO_REDIS_IGNORE_EXCEPTIONS = False
cache = cast(RedisCache, caches["doesnotexist"])
cache = cast("RedisCache", caches["doesnotexist"])
assert cache._ignore_exceptions is True
assert cache.get("key") is None

Expand All @@ -68,7 +68,7 @@ def test_get_django_omit_exceptions_priority_2(settings):
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = False
settings.CACHES = caches_setting
settings.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
cache = cast(RedisCache, caches["doesnotexist"])
cache = cast("RedisCache", caches["doesnotexist"])
assert cache._ignore_exceptions is False
with pytest.raises(ConnectionError):
cache.get("key")
Expand All @@ -84,7 +84,7 @@ def key_prefix_cache(cache: RedisCache, settings) -> Iterable[RedisCache]:

@pytest.fixture
def with_prefix_cache() -> Iterable[RedisCache]:
with_prefix = cast(RedisCache, caches["with_prefix"])
with_prefix = cast("RedisCache", caches["with_prefix"])
yield with_prefix
with_prefix.clear()

Expand Down
Loading