Skip to content

Commit 7127aa0

Browse files
authored
Merge pull request #39 from amirreza8002/cluster_methods
adjust cluster methods
2 parents ec9af24 + 7b1d4af commit 7127aa0

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

django_valkey/cluster_cache/cache.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,30 @@
55

66

77
class ClusterCommands:
8+
def msetnx(self: "ClusterValkeyCache", *args, **kwargs):
9+
return self.client.msetnx(*args, **kwargs)
10+
811
def mget_nonatomic(self: "ClusterValkeyCache", *args, **kwargs):
912
return self.client.mget_nonatomic(*args, **kwargs)
1013

1114
def mset_nonatomic(self: "ClusterValkeyCache", *args, **kwargs):
1215
return self.client.mset_nonatomic(*args, **kwargs)
1316

17+
def readonly(self: "ClusterValkeyCache", *args, **kwargs):
18+
return self.client.readonly(*args, **kwargs)
19+
20+
def readwrite(self: "ClusterValkeyCache", *args, **kwargs):
21+
return self.client.readwrite(*args, **kwargs)
22+
23+
def keyslot(self: "ClusterValkeyCache", *args, **kwargs):
24+
return self.client.keyslot(*args, **kwargs)
25+
26+
def flushall(self: "ClusterValkeyCache", *args, **kwargs):
27+
return self.client.flushall(*args, **kwargs)
28+
29+
def invalidate_key_from_cache(self: "ClusterValkeyCache", *args, **kwargs):
30+
return self.client.invalidate_key_from_cache(*args, **kwargs)
31+
1432

1533
class ClusterValkeyCache(
1634
BaseValkeyCache[DefaultClusterClient, ValkeyCluster],

django_valkey/cluster_cache/client/default.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,19 @@ def mget_nonatomic(self, keys, version=None, client=None):
9090
for key, value in zip(keys, values):
9191
if value is None:
9292
continue
93-
recovered_data[map_keys[key]] = self.decode(value)
93+
recovered_data[key] = self.decode(value)
9494
return recovered_data
9595

96+
get_many = mget_nonatomic
97+
9698
def keyslot(self, key, version=None, client=None):
9799
client = self._get_client(client=client)
98100
key = self.make_key(key, version=version)
99101
return client.keyslot(key)
100102

101-
def flush_cache(self, client=None):
103+
def flushall(self, asynchronous=False, client=None):
102104
client = self._get_client(client=client)
103-
return client.flush_cache()
105+
return client.flushall(asynchronous=asynchronous)
104106

105107
def invalidate_key_from_cache(self, client=None):
106108
client = self._get_client(client=client)

tests/tests_cluster/test_backend.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def test_mset(self, cache: ClusterValkeyCache):
4343
res = cache.mget(["a{foo}", "b{foo}", "c{foo}"])
4444
assert res == {"a{foo}": 1, "b{foo}": 2, "c{foo}": 3}
4545

46+
def test_msetnx(self, cache: ClusterValkeyCache):
47+
cache.mset({"a{foo}": 1, "b{foo}": 2, "c{foo}": 3})
48+
res = cache.mget(["a{foo}", "b{foo}", "c{foo}"])
49+
assert res == {"a{foo}": 1, "b{foo}": 2, "c{foo}": 3}
50+
cache.msetnx({"a{foo}": 3, "new{foo}": 1, "other{foo}": 1})
51+
res = cache.mget(["a{foo}", "new{foo}", "other{foo}"])
52+
assert res == {"a{foo}": 1}
53+
4654
def test_delete_pattern(self, cache: ClusterValkeyCache):
4755
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
4856
cache.set(key, "foo")
@@ -147,3 +155,12 @@ def test_sunionstore(self, cache: ClusterValkeyCache):
147155
cache.sadd("{foo}2", "bar2", "bar3")
148156
assert cache.sunionstore("{foo}3", "{foo}1", "{foo}2") == 3
149157
assert cache.smembers("{foo}3") == {"bar1", "bar2", "bar3"}
158+
159+
def test_flushall(self, cache: ClusterValkeyCache):
160+
cache.set("{foo}a", 1)
161+
cache.sadd("{foo}1", "bar1", "bar2")
162+
cache.hset("foo_hash1", "foo1", "bar1")
163+
cache.flushall()
164+
assert not cache.get("{foo}a")
165+
assert cache.smembers("{foo}a") == set()
166+
assert not cache.hexists("foo_hash1", "foo1")

0 commit comments

Comments
 (0)