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
9 changes: 9 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,15 @@ 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
is similar to GET, except for the fact that it also deletes
the key on success (if and only if the key's value type
is a string).
"""
return self.execute_command('GETDEL', name)

def __getitem__(self, name):
"""
Return the value at key ``name``, raises a KeyError if the key
Expand Down
7 changes: 7 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,13 @@ 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_getdel(self, r):
assert r.getdel('a') is None
assert r.set('a', 1)
AvitalFineRedis marked this conversation as resolved.
Show resolved Hide resolved
assert r.getdel('a') == b'1'
assert r.getdel('a') is None

def test_getitem_and_setitem(self, r):
r['a'] = 'bar'
assert r['a'] == b'bar'
Expand Down