Skip to content

Commit b3ee3e2

Browse files
Saurav SharmaSaurav Sharma
Saurav Sharma
authored and
Saurav Sharma
committed
Add option flags nx, xx, gt, lt for expire and pexpire
1 parent ad9cc78 commit b3ee3e2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

changelog.d/730.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add option flags nx, xx, gt, lt for expire and pexpire

django_redis/client/default.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ def expire(
298298
timeout: ExpiryT,
299299
version: Optional[int] = None,
300300
client: Optional[Redis] = None,
301+
nx: bool = False,
302+
xx: bool = False,
303+
gt: bool = False,
304+
lt: bool = False,
301305
) -> bool:
302306
if timeout is DEFAULT_TIMEOUT:
303307
timeout = self._backend.default_timeout # type: ignore
@@ -309,14 +313,18 @@ def expire(
309313

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

314318
def pexpire(
315319
self,
316320
key: KeyT,
317321
timeout: ExpiryT,
318322
version: Optional[int] = None,
319323
client: Optional[Redis] = None,
324+
nx: bool = False,
325+
xx: bool = False,
326+
gt: bool = False,
327+
lt: bool = False,
320328
) -> bool:
321329
if timeout is DEFAULT_TIMEOUT:
322330
timeout = self._backend.default_timeout # type: ignore
@@ -330,7 +338,7 @@ def pexpire(
330338
# is fixed.
331339
# for some strange reason mypy complains,
332340
# saying that timeout type is float | timedelta
333-
return bool(client.pexpire(key, timeout)) # type: ignore
341+
return bool(client.pexpire(key, timeout, nx, xx, gt, lt)) # type: ignore
334342

335343
def pexpire_at(
336344
self,

tests/test_backend.py

+12
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,12 @@ def test_expire(self, cache: RedisCache):
606606
ttl = cache.ttl("foo")
607607
assert pytest.approx(ttl) == 20
608608
assert cache.expire("not-existent-key", 20) is False
609+
cache.set("key1", "value1", timeout=None)
610+
assert cache.expire("key1", 20, nx=True) is True
611+
cache.set("key2", "value2", timeout=20)
612+
assert cache.expire("key2", 21, xx=True) is True
613+
assert cache.expire("key2", 30, gt=True) is True
614+
assert cache.expire("key2", 20, lt=True) is True
609615

610616
def test_expire_with_default_timeout(self, cache: RedisCache):
611617
cache.set("foo", "bar", timeout=None)
@@ -619,6 +625,12 @@ def test_pexpire(self, cache: RedisCache):
619625
# delta is set to 10 as precision error causes tests to fail
620626
assert pytest.approx(ttl, 10) == 20500
621627
assert cache.pexpire("not-existent-key", 20500) is False
628+
cache.set("key1", "value1", timeout=None)
629+
assert cache.pexpire("key1", 20000, nx=True) is True
630+
cache.set("key2", "value2", timeout=20000)
631+
assert cache.expire("key2", 20500, xx=True) is True
632+
assert cache.expire("key2", 30000, gt=True) is True
633+
assert cache.expire("key2", 20000, lt=True) is True
622634

623635
def test_pexpire_with_default_timeout(self, cache: RedisCache):
624636
cache.set("foo", "bar", timeout=None)

0 commit comments

Comments
 (0)