Skip to content

Commit 37e7c09

Browse files
authored
implementing the LMOVE and BLMOVE commands (#1504)
1 parent 2789f08 commit 37e7c09

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

redis/client.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,8 @@ class Redis:
581581
"""
582582
RESPONSE_CALLBACKS = {
583583
**string_keys_to_dict(
584-
'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST '
585-
'PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX',
584+
'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET LMOVE BLMOVE MOVE '
585+
'MSETNX PERSIST PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX',
586586
bool
587587
),
588588
**string_keys_to_dict(
@@ -1851,6 +1851,23 @@ def keys(self, pattern='*'):
18511851
"Returns a list of keys matching ``pattern``"
18521852
return self.execute_command('KEYS', pattern)
18531853

1854+
def lmove(self, first_list, second_list, src="LEFT", dest="RIGHT"):
1855+
"""
1856+
Atomically returns and removes the first/last element of a list,
1857+
pushing it as the first/last element on the destination list.
1858+
Returns the element being popped and pushed.
1859+
"""
1860+
params = [first_list, second_list, src, dest]
1861+
return self.execute_command("LMOVE", *params)
1862+
1863+
def blmove(self, first_list, second_list, timeout,
1864+
src="LEFT", dest="RIGHT"):
1865+
"""
1866+
Blocking version of lmove.
1867+
"""
1868+
params = [first_list, second_list, src, dest, timeout]
1869+
return self.execute_command("BLMOVE", *params)
1870+
18541871
def mget(self, keys, *args):
18551872
"""
18561873
Returns a list of values ordered identically to ``keys``

tests/test_commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,18 @@ def test_mget(self, r):
849849
r['c'] = '3'
850850
assert r.mget('a', 'other', 'b', 'c') == [b'1', None, b'2', b'3']
851851

852+
@skip_if_server_version_lt('6.2.0')
853+
def test_lmove(self, r):
854+
r.rpush('a', 'one', 'two', 'three', 'four')
855+
assert r.lmove('a', 'b')
856+
assert r.lmove('a', 'b', 'right', 'left')
857+
858+
@skip_if_server_version_lt('6.2.0')
859+
def test_blmove(self, r):
860+
r.rpush('a', 'one', 'two', 'three', 'four')
861+
assert r.blmove('a', 'b', 5)
862+
assert r.blmove('a', 'b', 1, 'RIGHT', 'LEFT')
863+
852864
def test_mset(self, r):
853865
d = {'a': b'1', 'b': b'2', 'c': b'3'}
854866
assert r.mset(d)

0 commit comments

Comments
 (0)