Skip to content

Commit f43c3e0

Browse files
Add support for PEXPIRE command's option (#2026)
* Add support for PEXPIRE command's option * Alter method arguments * add variables to the function header Co-authored-by: dvora-h <dvora.heller@redis.com>
1 parent eac3a34 commit f43c3e0

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

redis/commands/core.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,17 +1827,41 @@ def persist(self, name: KeyT) -> ResponseT:
18271827
"""
18281828
return self.execute_command("PERSIST", name)
18291829

1830-
def pexpire(self, name: KeyT, time: ExpiryT) -> ResponseT:
1830+
def pexpire(
1831+
self,
1832+
name: KeyT,
1833+
time: ExpiryT,
1834+
nx: bool = False,
1835+
xx: bool = False,
1836+
gt: bool = False,
1837+
lt: bool = False,
1838+
) -> ResponseT:
18311839
"""
1832-
Set an expire flag on key ``name`` for ``time`` milliseconds.
1833-
``time`` can be represented by an integer or a Python timedelta
1834-
object.
1840+
Set an expire flag on key ``name`` for ``time`` milliseconds
1841+
with given ``option``. ``time`` can be represented by an
1842+
integer or a Python timedelta object.
1843+
1844+
Valid options are:
1845+
NX -> Set expiry only when the key has no expiry
1846+
XX -> Set expiry only when the key has an existing expiry
1847+
GT -> Set expiry only when the new expiry is greater than current one
1848+
LT -> Set expiry only when the new expiry is less than current one
18351849
18361850
For more information check https://redis.io/commands/pexpire
18371851
"""
18381852
if isinstance(time, datetime.timedelta):
18391853
time = int(time.total_seconds() * 1000)
1840-
return self.execute_command("PEXPIRE", name, time)
1854+
1855+
exp_option = list()
1856+
if nx:
1857+
exp_option.append("NX")
1858+
if xx:
1859+
exp_option.append("XX")
1860+
if gt:
1861+
exp_option.append("GT")
1862+
if lt:
1863+
exp_option.append("LT")
1864+
return self.execute_command("PEXPIRE", name, time, *exp_option)
18411865

18421866
def pexpireat(self, name: KeyT, when: AbsExpiryT) -> ResponseT:
18431867
"""

tests/test_commands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,33 @@ def test_pexpire(self, r):
12961296
assert r.persist("a")
12971297
assert r.pttl("a") == -1
12981298

1299+
@skip_if_server_version_lt("7.0.0")
1300+
def test_pexpire_option_nx(self, r):
1301+
assert r.set("key", "val") is True
1302+
assert r.pexpire("key", 60000, nx=True) is True
1303+
assert r.pexpire("key", 60000, nx=True) is False
1304+
1305+
@skip_if_server_version_lt("7.0.0")
1306+
def test_pexpire_option_xx(self, r):
1307+
assert r.set("key", "val") is True
1308+
assert r.pexpire("key", 60000, xx=True) is False
1309+
assert r.pexpire("key", 60000) is True
1310+
assert r.pexpire("key", 70000, xx=True) is True
1311+
1312+
@skip_if_server_version_lt("7.0.0")
1313+
def test_pexpire_option_gt(self, r):
1314+
assert r.set("key", "val") is True
1315+
assert r.pexpire("key", 60000) is True
1316+
assert r.pexpire("key", 70000, gt=True) is True
1317+
assert r.pexpire("key", 50000, gt=True) is False
1318+
1319+
@skip_if_server_version_lt("7.0.0")
1320+
def test_pexpire_option_lt(self, r):
1321+
assert r.set("key", "val") is True
1322+
assert r.pexpire("key", 60000) is True
1323+
assert r.pexpire("key", 50000, lt=True) is True
1324+
assert r.pexpire("key", 70000, lt=True) is False
1325+
12991326
@skip_if_server_version_lt("2.6.0")
13001327
def test_pexpireat_datetime(self, r):
13011328
expire_at = redis_server_time(r) + datetime.timedelta(minutes=1)

0 commit comments

Comments
 (0)