Skip to content

implementing the LMOVE and BLMOVE commands #1504

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 13 commits into from
Aug 15, 2021
21 changes: 19 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ class Redis:
"""
RESPONSE_CALLBACKS = {
**string_keys_to_dict(
'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST '
'PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX',
'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET LMOVE BLMOVE MOVE '
'MSETNX PERSIST PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX',
bool
),
**string_keys_to_dict(
Expand Down Expand Up @@ -1834,6 +1834,23 @@ def keys(self, pattern='*'):
"Returns a list of keys matching ``pattern``"
return self.execute_command('KEYS', pattern)

def lmove(self, first_list, second_list, src="LEFT", dest="RIGHT"):
"""
Atomically returns and removes the first/last element of a list,
pushing it as the first/last element on the destination list.
Returns the element being popped and pushed.
"""
params = [first_list, second_list, src, dest]
return self.execute_command("LMOVE", *params)

def blmove(self, first_list, second_list, timeout,
src="LEFT", dest="RIGHT"):
"""
Blocking version of lmove.
"""
params = [first_list, second_list, src, dest, timeout]
return self.execute_command("BLMOVE", *params)

def mget(self, keys, *args):
"""
Returns a list of values ordered identically to ``keys``
Expand Down
12 changes: 12 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,18 @@ def test_mget(self, r):
r['c'] = '3'
assert r.mget('a', 'other', 'b', 'c') == [b'1', None, b'2', b'3']

@skip_if_server_version_lt('6.2.0')
def test_lmove(self, r):
r.rpush('a', 'one', 'two', 'three', 'four')
assert r.lmove('a', 'b')
assert r.lmove('a', 'b', 'right', 'left')

@skip_if_server_version_lt('6.2.0')
def test_blmove(self, r):
r.rpush('a', 'one', 'two', 'three', 'four')
assert r.blmove('a', 'b', 5)
assert r.blmove('a', 'b', 1, 'RIGHT', 'LEFT')

def test_mset(self, r):
d = {'a': b'1', 'b': b'2', 'c': b'3'}
assert r.mset(d)
Expand Down