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

Add option flags nx, xx, gt, lt for expire and pexpire #733

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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 changelog.d/730.feature
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Support for sets and support basic operations, sadd, scard, sdiff, sdiffstore, sinter, sinterstore, smismember, sismember, smembers, smove, spop, srandmember, srem, sscan, sscan_iter, sunion, sunionstore
Support for sets and support basic operations, sadd, scard, sdiff, sdiffstore, sinter, sinterstore, smismember, sismember, smembers, smove, spop, srandmember, srem, sscan, sscan_iter, sunion, sunionstore
1 change: 1 addition & 0 deletions changelog.d/731.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option flags nx, xx, gt, lt for expire and pexpire
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 @@ -328,14 +332,18 @@ def expire(

# for some strange reason mypy complains,
# saying that timeout type is float | timedelta
return client.expire(key, timeout) # type: ignore
return client.expire(key, timeout, nx, xx, gt, lt) # type: ignore

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 @@ -349,7 +357,7 @@ def pexpire(
# is fixed.
# for some strange reason mypy complains,
# saying that timeout type is float | timedelta
return bool(client.pexpire(key, timeout)) # type: ignore
return bool(client.pexpire(key, timeout, nx, xx, gt, lt)) # type: ignore

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 @@

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

def expire(self, key, timeout, version=None, client=None):
def expire(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about type hints?

self,
key,
timeout,
version=None,
client=None,
nx=False,
xx=False,
gt=False,
lt=False,

Check warning on line 183 in django_redis/client/sharded.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/sharded.py#L178-L183

Added lines #L178 - L183 were not covered by tests
):
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,

Check warning on line 197 in django_redis/client/sharded.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/sharded.py#L190-L197

Added lines #L190 - L197 were not covered by tests
)

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,

Check warning on line 209 in django_redis/client/sharded.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/sharded.py#L204-L209

Added lines #L204 - L209 were not covered by tests
):
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,

Check warning on line 223 in django_redis/client/sharded.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/sharded.py#L216-L223

Added lines #L216 - L223 were not covered by tests
)

def pexpire_at(self, key, when: Union[datetime, int], version=None, client=None):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@
ttl = cache.ttl("foo")
assert pytest.approx(ttl) == 20
assert cache.expire("not-existent-key", 20) is False
cache.set("key1", "value1", timeout=None)
assert cache.expire("key1", 20, nx=True) is True
cache.set("key2", "value2", timeout=20)
assert cache.expire("key2", 21, xx=True) is True
assert cache.expire("key2", 30, gt=True) is True
assert cache.expire("key2", 20, lt=True) is True

Check warning on line 614 in tests/test_backend.py

View check run for this annotation

Codecov / codecov/patch

tests/test_backend.py#L609-L614

Added lines #L609 - L614 were not covered by tests
Comment on lines +609 to +614
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to check also ttl after, please add separate tests or use parametrize


def test_expire_with_default_timeout(self, cache: RedisCache):
cache.set("foo", "bar", timeout=None)
Expand All @@ -619,6 +625,12 @@
# delta is set to 10 as precision error causes tests to fail
assert pytest.approx(ttl, 10) == 20500
assert cache.pexpire("not-existent-key", 20500) is False
cache.set("key1", "value1", timeout=None)
assert cache.pexpire("key1", 20000, nx=True) is True
cache.set("key2", "value2", timeout=20000)
assert cache.expire("key2", 20500, xx=True) is True
assert cache.expire("key2", 30000, gt=True) is True
assert cache.expire("key2", 20000, lt=True) is True

Check warning on line 633 in tests/test_backend.py

View check run for this annotation

Codecov / codecov/patch

tests/test_backend.py#L628-L633

Added lines #L628 - L633 were not covered by tests
Comment on lines +628 to +633
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the above


def test_pexpire_with_default_timeout(self, cache: RedisCache):
cache.set("foo", "bar", timeout=None)
Expand Down
Loading