Skip to content

Commit

Permalink
implementing the LMOVE and BLMOVE commands (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
chayim authored Aug 15, 2021
1 parent 2789f08 commit 37e7c09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
21 changes: 19 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,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 @@ -1851,6 +1851,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

0 comments on commit 37e7c09

Please sign in to comment.