From 37e7c093cbeaf7133d5dd7189563d5c15da8d12b Mon Sep 17 00:00:00 2001 From: Chayim Date: Sun, 15 Aug 2021 13:44:41 +0300 Subject: implementing the LMOVE and BLMOVE commands (#1504) --- redis/client.py | 21 +++++++++++++++++++-- tests/test_commands.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index 3c3ab7f..a80012a 100755 --- a/redis/client.py +++ b/redis/client.py @@ -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( @@ -1851,6 +1851,23 @@ class Redis: "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`` diff --git a/tests/test_commands.py b/tests/test_commands.py index 76322d7..736aec9 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -849,6 +849,18 @@ class TestRedisCommands: 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) -- cgit v1.2.1