diff options
author | dogukanteber <47397379+dogukanteber@users.noreply.github.com> | 2022-03-14 16:02:46 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-14 15:02:46 +0200 |
commit | 2c405855021ccc8d4a45fa1aae352ddb303baae0 (patch) | |
tree | d54d448d753df08301a90a2b67e9b696ef31b9c3 /redis/commands/core.py | |
parent | a12b5fd5e9af55e4668c78b50388c4ffb73fdda0 (diff) | |
download | redis-py-2c405855021ccc8d4a45fa1aae352ddb303baae0.tar.gz |
Add support for EXPIRE command's options (#2002)
* Add support for EXPIRE command's options
* Add requested changes
* Change method arguments
* add variables to the function header
Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
Co-authored-by: Chayim <chayim@users.noreply.github.com>
Co-authored-by: dvora-h <dvora.heller@redis.com>
Diffstat (limited to 'redis/commands/core.py')
-rw-r--r-- | redis/commands/core.py | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py index 3595677..108da1b 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1501,16 +1501,42 @@ class BasicKeyCommands(CommandsProtocol): __contains__ = exists - def expire(self, name: KeyT, time: ExpiryT) -> ResponseT: + def expire( + 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`` seconds. ``time`` - can be represented by an integer or a Python timedelta object. + Set an expire flag on key ``name`` for ``time`` seconds 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/expire """ if isinstance(time, datetime.timedelta): time = int(time.total_seconds()) - return self.execute_command("EXPIRE", 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("EXPIRE", name, time, *exp_option) def expireat(self, name: KeyT, when: AbsExpiryT) -> ResponseT: """ |