Skip to content
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

getdel #1514

Merged
merged 4 commits into from
Jul 25, 2021
Merged

getdel #1514

Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into GETDEL
  • Loading branch information
AvitalFineRedis authored Jul 22, 2021
commit 9bb31c0baff001d7d685f7eec2bfc81990d454aa
52 changes: 52 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,7 @@ def get(self, name):
"""
return self.execute_command('GET', name)


def getdel(self, name):
"""
Get the value at key ``name`` and delete the key. This command
Expand All @@ -1706,6 +1707,57 @@ def getdel(self, name):
"""
return self.execute_command('GETDEL', 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 @@ -739,6 +739,21 @@ def test_getdel(self, r):
r.set('a', 1)
assert r.getdel('a') == b'1'
assert r.getdel('a') is None

@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'
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.