From fdf4f1adb9e2636a04343a3bfade2abac6877197 Mon Sep 17 00:00:00 2001 From: dogukanteber <47397379+dogukanteber@users.noreply.github.com> Date: Mon, 14 Mar 2022 16:03:40 +0300 Subject: Add support for PEXPIREAT's options (#2027) * Add support for PEXPIREAT's options * add variables to the function header. Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com> Co-authored-by: dvora-h --- redis/commands/core.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'redis/commands/core.py') diff --git a/redis/commands/core.py b/redis/commands/core.py index 7e3514e..56d6fa9 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1863,18 +1863,41 @@ class BasicKeyCommands(CommandsProtocol): exp_option.append("LT") return self.execute_command("PEXPIRE", name, time, *exp_option) - def pexpireat(self, name: KeyT, when: AbsExpiryT) -> ResponseT: + def pexpireat( + self, + name: KeyT, + when: AbsExpiryT, + nx: bool = False, + xx: bool = False, + gt: bool = False, + lt: bool = False, + ) -> ResponseT: """ - Set an expire flag on key ``name``. ``when`` can be represented - as an integer representing unix time in milliseconds (unix time * 1000) - or a Python datetime object. + Set an expire flag on key ``name`` with given ``option``. ``when`` + can be represented as an integer representing unix time in + milliseconds (unix time * 1000) or a Python datetime 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/pexpireat """ if isinstance(when, datetime.datetime): ms = int(when.microsecond / 1000) when = int(time.mktime(when.timetuple())) * 1000 + ms - return self.execute_command("PEXPIREAT", name, when) + 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("PEXPIREAT", name, when, *exp_option) def pexpiretime(self, key: str) -> int: """ -- cgit v1.2.1