Skip to content

getex #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2021
Merged

getex #1515

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,57 @@ def get(self, name):
"""
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
Expand Down
15 changes: 15 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,21 @@ def test_get_and_set(self, r):
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'
Expand Down