Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : add 'hset, hdel' interfaces #618

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d5919c1
feat : add 'hset, hdel' interfaces
Aug 11, 2022
3cd84e5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
8bae96c
feat : add 'hset, hdel' interfaces\nfix : type hints
Aug 11, 2022
8fa089f
fix - type hints
Aug 11, 2022
00aaaf6
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Aug 11, 2022
fc2a908
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
b8aa932
fix - return type , changelog filename format
Aug 11, 2022
9e89683
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Aug 11, 2022
cd82002
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
ff99a25
fix - return type , changelog filename format
Aug 11, 2022
66e00b9
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Aug 11, 2022
df94dc5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
7fe3947
fix - type hint
Aug 11, 2022
d484117
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Aug 11, 2022
b368de0
fix : lint
Aug 11, 2022
f01d2d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
3e37109
feat : add hget interfaces and write tests
Sep 13, 2022
e3542ef
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Sep 13, 2022
e6b301f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2022
432e1c9
fix : delete print
Sep 13, 2022
17de9f5
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Sep 13, 2022
c0215ca
fix : tabs and flake E711(comparison to None)
Sep 13, 2022
9d041f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2022
5077eba
feat : add sharded interface
Sep 13, 2022
6a7c101
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Sep 13, 2022
85e5deb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2022
10f5ad6
feat : hdel receive List
Sep 13, 2022
306d780
Merge branch 'hash-set-delete' of https://github.com/moonseoklee/djan…
Sep 13, 2022
b6115c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2022
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
1 change: 1 addition & 0 deletions changelog.d/373.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The interface corresponding to hset, hdel of redis-py has been created.
12 changes: 12 additions & 0 deletions django_redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,15 @@ def close(self, **kwargs):
@omit_exception
def touch(self, *args, **kwargs):
return self.client.touch(*args, **kwargs)

@omit_exception
def hdel(self, *args, **kwargs):
return self.client.hdel(*args, **kwargs)

@omit_exception
def hset(self, *args, **kwargs):
return self.client.hset(*args, **kwargs)

@omit_exception
def hget(self, *args, **kwargs):
return self.client.hget(*args, **kwargs)
71 changes: 71 additions & 0 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,74 @@ def touch(
# Convert to milliseconds
timeout = int(timeout * 1000)
return bool(client.pexpire(key, timeout))

def hdel(
self,
name: Any,
keys: List,
client: Optional[Redis] = None,
version: Optional[int] = None,
) -> bool:
name = self.make_key(name, version=version)
if client is None:
client = self.get_client(write=True)
try:
return bool(client.hdel(name, *keys))
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client) from e

def hset(
self,
name: Any,
key: Union[str, bytes],
value: Union[bytes, float, int, str],
version: Optional[int] = None,
mapping: Optional[dict] = None,
client: Optional[Redis] = None,
) -> bool:

original_client = client
tried = [] # type: List[int]
name = self.make_key(name, version=version)
value = self.encode(value)
while True:
try:
if client is None:
client, index = self.get_client(
write=True, tried=tried, show_index=True
)

return bool(client.hset(name, key, value, mapping))
except _main_exceptions as e:
if (
not original_client
and not self._replica_read_only
and len(tried) < len(self._server)
):
tried.append(index)
client = None
continue
raise ConnectionInterrupted(connection=client) from e

def hget(
self,
name: str,
key: str,
default=None,
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> Any:
if client is None:
client = self.get_client(write=False)

name = self.make_key(name, version=version)

try:
value = client.hget(name, key)
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client) from e

if value is None:
return default

return self.decode(value)
35 changes: 35 additions & 0 deletions django_redis/client/sharded.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,38 @@ def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None, client=None):
def clear(self, client=None):
for connection in self._serverdict.values():
connection.flushdb()

def hset(
self, name, key, value, timeout=DEFAULT_TIMEOUT, version=None, client=None
):
"""
Persist a value to the cache, and set an optional expiration time.
"""
if client is None:
name = self.make_key(name, version=version)
client = self.get_server(name)

return super().hset(
name=name, key=key, value=value, version=version, client=client
)

def hget(self, name, key, version=None, client=None):
"""
Persist a value to the cache, and set an optional expiration time.
"""
if client is None:
name = self.make_key(name, version=version)
client = self.get_server(name)

return super().hget(name=name, key=key, version=version, client=client)

def hdel(self, name, keys, version=None, client=None):
if client is None:
name = self.make_key(name, version=version)
client = self.get_server(name)
return super().hdel(
name=name,
keys=keys,
version=version,
client=client,
)
15 changes: 15 additions & 0 deletions tests/test_backend.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,18 @@ def test_clear(self, cache: RedisCache):
cache.clear()
value_from_cache_after_clear = cache.get("foo")
assert value_from_cache_after_clear is None

def test_hset_hget(self, cache: RedisCache):
cache.hset("foo", "bar", "baz")
cache.hset("foo", "baz", "bar")
value_from_cache = cache.hget("foo", "bar")
assert value_from_cache == "baz"
value_from_cache = cache.hget("foo", "baz")
assert value_from_cache == "bar"

def test_hdel(self, cache: RedisCache):
cache.hset("foo", "bar", "baz")
cache.hset("foo", "baz", "bar")
cache.hdel("foo", ["bar", "baz"])
assert cache.hget("foo", "bar") is None
assert cache.hget("foo", "baz") is None