diff options
author | Chayim <chayim@users.noreply.github.com> | 2021-08-15 13:44:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-15 13:44:41 +0300 |
commit | 37e7c093cbeaf7133d5dd7189563d5c15da8d12b (patch) | |
tree | 7b54fa0398b5273ab1214b69c3c32bc33f79e760 | |
parent | 2789f08dfb7192f112aa0fc8cec6738dfb1b3a08 (diff) | |
download | redis-py-37e7c093cbeaf7133d5dd7189563d5c15da8d12b.tar.gz |
implementing the LMOVE and BLMOVE commands (#1504)
-rwxr-xr-x | redis/client.py | 21 | ||||
-rw-r--r-- | 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) |