Repository: redis/redis-py

Issue:
Add support for PEXPIRE redis-7 options [NX|XX|GT|LT]



Discussion:

Resolving patch:
diff --git a/redis/commands/core.py b/redis/commands/core.py
--- a/redis/commands/core.py
+++ b/redis/commands/core.py
@@ -1775,17 +1775,41 @@ def persist(self, name: KeyT) -> ResponseT:
         """
         return self.execute_command("PERSIST", name)
 
-    def pexpire(self, name: KeyT, time: ExpiryT) -> ResponseT:
+    def pexpire(
+        self,
+        name: KeyT,
+        time: ExpiryT,
+        nx: bool = False,
+        xx: bool = False,
+        gt: bool = False,
+        lt: bool = False,
+    ) -> ResponseT:
         """
-        Set an expire flag on key ``name`` for ``time`` milliseconds.
-        ``time`` can be represented by an integer or a Python timedelta
-        object.
+        Set an expire flag on key ``name`` for ``time`` milliseconds
+        with given ``option``. ``time`` can be represented by an
+        integer or a Python timedelta object.
+
+        Valid options are:
+            NX -> Set expiry only when the key has no expiry
+            XX -> Set expiry only when the key has an existing expiry
+            GT -> Set expiry only when the new expiry is greater than current one
+            LT -> Set expiry only when the new expiry is less than current one
 
         For more information check https://redis.io/commands/pexpire
         """
         if isinstance(time, datetime.timedelta):
             time = int(time.total_seconds() * 1000)
-        return self.execute_command("PEXPIRE", name, time)
+
+        exp_option = list()
+        if nx:
+            exp_option.append("NX")
+        if xx:
+            exp_option.append("XX")
+        if gt:
+            exp_option.append("GT")
+        if lt:
+            exp_option.append("LT")
+        return self.execute_command("PEXPIRE", name, time, *exp_option)
 
     def pexpireat(self, name: KeyT, when: AbsExpiryT) -> ResponseT:
         """


Test patch:
diff --git a/tests/test_commands.py b/tests/test_commands.py
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -1234,6 +1234,33 @@ def test_pexpire(self, r):
         assert r.persist("a")
         assert r.pttl("a") == -1
 
+    @skip_if_server_version_lt("7.0.0")
+    def test_pexpire_option_nx(self, r):
+        assert r.set("key", "val") is True
+        assert r.pexpire("key", 60000, nx=True) is True
+        assert r.pexpire("key", 60000, nx=True) is False
+
+    @skip_if_server_version_lt("7.0.0")
+    def test_pexpire_option_xx(self, r):
+        assert r.set("key", "val") is True
+        assert r.pexpire("key", 60000, xx=True) is False
+        assert r.pexpire("key", 60000) is True
+        assert r.pexpire("key", 70000, xx=True) is True
+
+    @skip_if_server_version_lt("7.0.0")
+    def test_pexpire_option_gt(self, r):
+        assert r.set("key", "val") is True
+        assert r.pexpire("key", 60000) is True
+        assert r.pexpire("key", 70000, gt=True) is True
+        assert r.pexpire("key", 50000, gt=True) is False
+
+    @skip_if_server_version_lt("7.0.0")
+    def test_pexpire_option_lt(self, r):
+        assert r.set("key", "val") is True
+        assert r.pexpire("key", 60000) is True
+        assert r.pexpire("key", 50000, lt=True) is True
+        assert r.pexpire("key", 70000, lt=True) is False
+
     @skip_if_server_version_lt("2.6.0")
     def test_pexpireat_datetime(self, r):
         expire_at = redis_server_time(r) + datetime.timedelta(minutes=1)