Skip to content

Commit 4f815b8

Browse files
committed
Add support for hashmaps
1 parent d94a7f9 commit 4f815b8

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

changelog.d/598.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support HashMaps

django_redis/cache.py

+20
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,23 @@ def close(self, **kwargs):
184184
@omit_exception
185185
def touch(self, *args, **kwargs):
186186
return self.client.touch(*args, **kwargs)
187+
188+
@omit_exception
189+
def hset(self, *args, **kwargs):
190+
return self.client.hset(*args, **kwargs)
191+
192+
@omit_exception
193+
def hdel(self, *args, **kwargs):
194+
return self.client.hdel(*args, **kwargs)
195+
196+
@omit_exception
197+
def hlen(self, *args, **kwargs):
198+
return self.client.hlen(*args, **kwargs)
199+
200+
@omit_exception
201+
def hkeys(self, *args, **kwargs):
202+
return self.client.hkeys(*args, **kwargs)
203+
204+
@omit_exception
205+
def hexists(self, *args, **kwargs):
206+
return self.client.hexists(*args, **kwargs)

django_redis/client/default.py

+70
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,73 @@ def touch(
813813
# Convert to milliseconds
814814
timeout = int(timeout * 1000)
815815
return bool(client.pexpire(key, timeout))
816+
817+
def hset(
818+
self,
819+
name: str,
820+
key: Any,
821+
value: Any,
822+
version: Optional[int] = None,
823+
client: Optional[Redis] = None,
824+
) -> int:
825+
"""
826+
Set the value of hash name at key to value.
827+
"""
828+
if client is None:
829+
client = self.get_client(write=True)
830+
key = self.make_key(key, version=version)
831+
return int(client.hset(name, key, value))
832+
833+
def hdel(
834+
self,
835+
name: str,
836+
key: Any,
837+
version: Optional[int] = None,
838+
client: Optional[Redis] = None,
839+
) -> int:
840+
"""
841+
Remove keys from hash name.
842+
"""
843+
if client is None:
844+
client = self.get_client(write=True)
845+
key = self.make_key(key, version=version)
846+
return int(client.hdel(name, key))
847+
848+
def hlen(
849+
self,
850+
name: str,
851+
client: Optional[Redis] = None,
852+
) -> int:
853+
"""
854+
Return the number of items in hash name.
855+
"""
856+
if client is None:
857+
client = self.get_client(write=False)
858+
return int(client.hlen(name))
859+
860+
def hkeys(
861+
self,
862+
name: str,
863+
client: Optional[Redis] = None,
864+
) -> List:
865+
"""
866+
Return a list of keys in hash name.
867+
"""
868+
if client is None:
869+
client = self.get_client(write=False)
870+
return client.hkeys(name)
871+
872+
def hexists(
873+
self,
874+
name: str,
875+
key: Any,
876+
version: Optional[int] = None,
877+
client: Optional[Redis] = None,
878+
) -> bool:
879+
"""
880+
Return True if key exists in hash name, else False.
881+
"""
882+
if client is None:
883+
client = self.get_client(write=False)
884+
key = self.make_key(key, version=version)
885+
return bool(client.hexists(name, key))

tests/test_backend.py

+35
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,38 @@ def test_clear(self, cache: RedisCache):
797797
cache.clear()
798798
value_from_cache_after_clear = cache.get("foo")
799799
assert value_from_cache_after_clear is None
800+
801+
def test_hset(self, cache: RedisCache):
802+
cache.hset("foo_hash1", "foo1", "bar1")
803+
cache.hset("foo_hash1", "foo2", "bar2")
804+
assert cache.hlen("foo_hash1") == 2
805+
806+
def test_hdel(self, cache: RedisCache):
807+
cache.hset("foo_hash2", "foo1", "bar1")
808+
cache.hset("foo_hash2", "foo2", "bar2")
809+
assert cache.hlen("foo_hash2") == 2
810+
cache.hdel("foo_hash2", "foo1")
811+
assert cache.hlen("foo_hash2") == 1
812+
assert not cache.hexists("foo_hash2", "foo1")
813+
assert cache.hexists("foo_hash2", "foo2")
814+
815+
def test_hlen(self, cache: RedisCache):
816+
assert cache.hlen("foo_hash3") == 0
817+
cache.hset("foo_hash3", "foo1", "bar1")
818+
assert cache.hlen("foo_hash3") == 1
819+
cache.hset("foo_hash3", "foo2", "bar2")
820+
assert cache.hlen("foo_hash3") == 2
821+
822+
def test_hkeys(self, cache: RedisCache):
823+
cache.hset("foo_hash4", "foo1", "bar1")
824+
cache.hset("foo_hash4", "foo2", "bar2")
825+
cache.hset("foo_hash4", "foo3", "bar3")
826+
keys = cache.hkeys("foo_hash4")
827+
assert len(keys) == 3
828+
for i in range(len(keys)):
829+
assert keys[i] == f"foo{i + 1}"
830+
831+
def test_hexists(self, cache: RedisCache):
832+
cache.hset("foo_hash5", "foo1", "bar1")
833+
assert cache.hexists("foo_hash5", "foo1")
834+
assert not cache.hexists("foo_hash5", "foo")

0 commit comments

Comments
 (0)