Skip to content

Commit ced8968

Browse files
committed
Add support for PEXPIRE command's option
1 parent afc83e1 commit ced8968

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

redis/commands/core.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1755,17 +1755,26 @@ def persist(self, name: KeyT) -> ResponseT:
17551755
"""
17561756
return self.execute_command("PERSIST", name)
17571757

1758-
def pexpire(self, name: KeyT, time: ExpiryT) -> ResponseT:
1758+
def pexpire(self, name: KeyT, time: ExpiryT, option: str = None) -> ResponseT:
17591759
"""
1760-
Set an expire flag on key ``name`` for ``time`` milliseconds.
1761-
``time`` can be represented by an integer or a Python timedelta
1762-
object.
1760+
Set an expire flag on key ``name`` for ``time`` milliseconds
1761+
with given ``option``. ``time`` can be represented by an
1762+
integer or a Python timedelta object.
1763+
1764+
Valid options are:
1765+
NX -> Set expiry only when the key has no expiry
1766+
XX -> Set expiry only when the key has an existing expiry
1767+
GT -> Set expiry only when the new expiry is greater than current one
1768+
LT -> Set expiry only when the new expiry is less than current one
17631769
17641770
For more information check https://redis.io/commands/pexpire
17651771
"""
17661772
if isinstance(time, datetime.timedelta):
17671773
time = int(time.total_seconds() * 1000)
1768-
return self.execute_command("PEXPIRE", name, time)
1774+
exp_option = list()
1775+
if option is not None:
1776+
exp_option.append(option)
1777+
return self.execute_command("PEXPIRE", name, time, *exp_option)
17691778

17701779
def pexpireat(self, name: KeyT, when: AbsExpiryT) -> ResponseT:
17711780
"""

tests/test_commands.py

+27
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,33 @@ def test_pexpire(self, r):
12121212
assert r.persist("a")
12131213
assert r.pttl("a") == -1
12141214

1215+
@skip_if_server_version_lt("7.0.0")
1216+
def test_pexpire_option_nx(self, r):
1217+
assert r.set("key", "val") is True
1218+
assert r.pexpire("key", 60000, "NX") is True
1219+
assert r.pexpire("key", 60000, "NX") is False
1220+
1221+
@skip_if_server_version_lt("7.0.0")
1222+
def test_pexpire_option_xx(self, r):
1223+
assert r.set("key", "val") is True
1224+
assert r.pexpire("key", 60000, "XX") is False
1225+
assert r.pexpire("key", 60000) is True
1226+
assert r.pexpire("key", 70000, "XX") is True
1227+
1228+
@skip_if_server_version_lt("7.0.0")
1229+
def test_pexpire_option_gt(self, r):
1230+
assert r.set("key", "val") is True
1231+
assert r.pexpire("key", 60000) is True
1232+
assert r.pexpire("key", 70000, "GT") is True
1233+
assert r.pexpire("key", 50000, "GT") is False
1234+
1235+
@skip_if_server_version_lt("7.0.0")
1236+
def test_pexpire_option_lt(self, r):
1237+
assert r.set("key", "val") is True
1238+
assert r.pexpire("key", 60000) is True
1239+
assert r.pexpire("key", 50000, "LT") is True
1240+
assert r.pexpire("key", 70000, "LT") is False
1241+
12151242
@skip_if_server_version_lt("2.6.0")
12161243
def test_pexpireat_datetime(self, r):
12171244
expire_at = redis_server_time(r) + datetime.timedelta(minutes=1)

0 commit comments

Comments
 (0)