Skip to content

Commit

Permalink
Add support for nx, xx, gt, lt
Browse files Browse the repository at this point in the history
  • Loading branch information
whoiskrtk2 committed Nov 16, 2024
1 parent fccdcf8 commit 7dd5c1e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
12 changes: 10 additions & 2 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def expire(
timeout: ExpiryT,
version: Optional[int] = None,
client: Optional[Redis] = None,
nx: bool = False,
xx: bool = False,
gt: bool = False,
lt: bool = False,
) -> bool:
if timeout is DEFAULT_TIMEOUT:
timeout = self._backend.default_timeout # type: ignore
Expand All @@ -326,14 +330,18 @@ def expire(

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

return client.expire(key, timeout)
return client.expire(key, timeout, nx, xx, gt, lt)

def pexpire(
self,
key: KeyT,
timeout: ExpiryT,
version: Optional[int] = None,
client: Optional[Redis] = None,
nx: bool = False,
xx: bool = False,
gt: bool = False,
lt: bool = False,
) -> bool:
if timeout is DEFAULT_TIMEOUT:
timeout = self._backend.default_timeout # type: ignore
Expand All @@ -343,7 +351,7 @@ def pexpire(

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

return bool(client.pexpire(key, timeout))
return bool(client.pexpire(key, timeout, nx, xx, gt, lt))

def pexpire_at(
self,
Expand Down
46 changes: 42 additions & 4 deletions django_redis/client/sharded.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,57 @@ def persist(self, key, version=None, client=None):

return super().persist(key=key, version=version, client=client)

def expire(self, key, timeout, version=None, client=None):
def expire(
self,
key,
timeout,
version=None,
client=None,
nx=False,
xx=False,
gt=False,
lt=False,
):
if client is None:
key = self.make_key(key, version=version)
client = self.get_server(key)

return super().expire(key=key, timeout=timeout, version=version, client=client)
return super().expire(
key=key,
timeout=timeout,
version=version,
client=client,
nx=nx,
xx=xx,
gt=gt,
lt=lt,
)

def pexpire(self, key, timeout, version=None, client=None):
def pexpire(
self,
key,
timeout,
version=None,
client=None,
nx=False,
xx=False,
gt=False,
lt=False,
):
if client is None:
key = self.make_key(key, version=version)
client = self.get_server(key)

return super().pexpire(key=key, timeout=timeout, version=version, client=client)
return super().pexpire(
key=key,
timeout=timeout,
version=version,
client=client,
nx=nx,
xx=xx,
gt=gt,
lt=lt,
)

def pexpire_at(self, key, when: Union[datetime, int], version=None, client=None):
"""
Expand Down

0 comments on commit 7dd5c1e

Please sign in to comment.