summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvital Fine <79420960+AvitalFineRedis@users.noreply.github.com>2021-07-22 18:50:24 +0300
committerGitHub <noreply@github.com>2021-07-22 18:50:24 +0300
commit3c244af9e7c820d38135562d9385753f5bbb60e6 (patch)
tree35803065386eb5d53ee9d97704fa9cfcc11cf99e
parent9ce7bb2858065c0dc5ac91287b8716def9919291 (diff)
downloadredis-py-3c244af9e7c820d38135562d9385753f5bbb60e6.tar.gz
getex (#1515)
* getex * flake8 fix * comments
-rwxr-xr-xredis/client.py51
-rw-r--r--tests/test_commands.py15
2 files changed, 66 insertions, 0 deletions
diff --git a/redis/client.py b/redis/client.py
index 72178f3..8b1b353 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -1689,6 +1689,57 @@ class Redis:
"""
return self.execute_command('GET', name)
+ def getex(self, name,
+ ex=None, px=None, exat=None, pxat=None, persist=False):
+ """
+ Get the value of key and optionally set its expiration.
+ GETEX is similar to GET, but is a write command with
+ additional options. All time parameters can be given as
+ datetime.timedelta or integers.
+
+ ``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.
+
+ ``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.
+
+ ``exat`` sets an expire flag on key ``name`` for ``ex`` seconds,
+ specified in unix time.
+
+ ``pxat`` sets an expire flag on key ``name`` for ``ex`` milliseconds,
+ specified in unix time.
+
+ ``persist`` remove the time to live associated with ``name``.
+ """
+
+ pieces = []
+ # similar to set command
+ if ex is not None:
+ pieces.append('EX')
+ if isinstance(ex, datetime.timedelta):
+ ex = int(ex.total_seconds())
+ pieces.append(ex)
+ if px is not None:
+ pieces.append('PX')
+ if isinstance(px, datetime.timedelta):
+ px = int(px.total_seconds() * 1000)
+ pieces.append(px)
+ # similar to pexpireat command
+ if exat is not None:
+ pieces.append('EXAT')
+ if isinstance(exat, datetime.datetime):
+ s = int(exat.microsecond / 1000000)
+ exat = int(mod_time.mktime(exat.timetuple())) + s
+ pieces.append(exat)
+ if pxat is not None:
+ pieces.append('PXAT')
+ if isinstance(pxat, datetime.datetime):
+ ms = int(pxat.microsecond / 1000)
+ pxat = int(mod_time.mktime(pxat.timetuple())) * 1000 + ms
+ pieces.append(pxat)
+ if persist:
+ pieces.append('PERSIST')
+
+ return self.execute_command('GETEX', name, *pieces)
+
def __getitem__(self, name):
"""
Return the value at key ``name``, raises a KeyError if the key
diff --git a/tests/test_commands.py b/tests/test_commands.py
index a5a7e45..088404a 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -727,6 +727,21 @@ class TestRedisCommands:
assert r.get('integer') == str(integer).encode()
assert r.get('unicode_string').decode('utf-8') == unicode_string
+ @skip_if_server_version_lt('6.2.0')
+ def test_getex(self, r):
+ r.set('a', 1)
+ assert r.getex('a') == b'1'
+ assert r.ttl('a') == -1
+ assert r.getex('a', ex=60) == b'1'
+ assert r.ttl('a') == 60
+ assert r.getex('a', px=6000) == b'1'
+ assert r.ttl('a') == 6
+ expire_at = redis_server_time(r) + datetime.timedelta(minutes=1)
+ assert r.getex('a', pxat=expire_at) == b'1'
+ assert r.ttl('a') <= 60
+ assert r.getex('a', persist=True) == b'1'
+ assert r.ttl('a') == -1
+
def test_getitem_and_setitem(self, r):
r['a'] = 'bar'
assert r['a'] == b'bar'