diff options
| author | Itamar Haber <itamar@redislabs.com> | 2018-10-05 17:59:05 +0300 |
|---|---|---|
| committer | Itamar Haber <itamar@redislabs.com> | 2018-10-05 17:59:05 +0300 |
| commit | b2f61090f442468460d6968aee3ddea556b9b846 (patch) | |
| tree | 9e67d2022366c8b2d51b597961218d147076f94e | |
| parent | 66eefc6f52e9130e61dad251438352964946c786 (diff) | |
| download | redis-py-b2f61090f442468460d6968aee3ddea556b9b846.tar.gz | |
Adds BZPOPMAX and BZPOPMIN
Signed-off-by: Itamar Haber <itamar@redislabs.com>
| -rwxr-xr-x | redis/client.py | 42 | ||||
| -rw-r--r-- | tests/test_commands.py | 30 |
2 files changed, 71 insertions, 1 deletions
diff --git a/redis/client.py b/redis/client.py index 95e9438..535a59b 100755 --- a/redis/client.py +++ b/redis/client.py @@ -385,7 +385,7 @@ class StrictRedis(object): 'SAVE SELECT SHUTDOWN SLAVEOF WATCH UNWATCH', bool_ok ), - string_keys_to_dict('BLPOP BRPOP', lambda r: r and tuple(r) or None), + string_keys_to_dict('BLPOP BRPOP BZPOPMIN BZPOPMAX', lambda r: r and tuple(r) or None), string_keys_to_dict( 'SDIFF SINTER SMEMBERS SUNION', lambda r: r and set(r) or set() @@ -1744,6 +1744,46 @@ class StrictRedis(object): } return self.execute_command('ZPOPMIN', name, *args, **options) + def bzpopmax(self, keys, timeout=0): + """ + ZPOPMAX a value off of the first non-empty sorted set + named in the ``keys`` list. + + If none of the sorted sets in ``keys`` has a value to ZPOPMAX, + then block for ``timeout`` seconds, or until a member gets added + to one of the sorted sets. + + If timeout is 0, then block indefinitely. + """ + if timeout is None: + timeout = 0 + if isinstance(keys, basestring): + keys = [keys] + else: + keys = list(keys) + keys.append(timeout) + return self.execute_command('BZPOPMAX', *keys) + + def bzpopmin(self, keys, timeout=0): + """ + ZPOPMIN a value off of the first non-empty sorted set + named in the ``keys`` list. + + If none of the sorted sets in ``keys`` has a value to ZPOPMIN, + then block for ``timeout`` seconds, or until a member gets added + to one of the sorted sets. + + If timeout is 0, then block indefinitely. + """ + if timeout is None: + timeout = 0 + if isinstance(keys, basestring): + keys = [keys] + else: + keys = list(keys) + keys.append(timeout) + return self.execute_command('BZPOPMIN', *keys) + def zrange(self, name, start, end, desc=False, withscores=False, score_cast_func=float): """ diff --git a/tests/test_commands.py b/tests/test_commands.py index a3e1a33..1a5b426 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -989,6 +989,36 @@ class TestRedisCommands(object): assert r.zpopmin('a', count=2) == \ [(b('a2'), 2), (b('a3'), 3)] + @skip_if_server_version_lt('4.9.0') + def test_bzpopmax(self, r): + r.delete('a') + r.delete('b') + r.delete('c') + r.zadd('a', a1=1, a2=2) + r.zadd('b', b1=10, b2=20) + assert r.bzpopmax(['b', 'a'], timeout=1) == (b('b'), b('b2'), b('20')) + assert r.bzpopmax(['b', 'a'], timeout=1) == (b('b'), b('b1'), b('10')) + assert r.bzpopmax(['b', 'a'], timeout=1) == (b('a'), b('a2'), b('2')) + assert r.bzpopmax(['b', 'a'], timeout=1) == (b('a'), b('a1'), b('1')) + assert r.bzpopmax(['b', 'a'], timeout=1) is None + r.zadd('c', c1=100) + assert r.bzpopmax('c', timeout=1) == (b('c'), b('c1'), b('100')) + + @skip_if_server_version_lt('4.9.0') + def test_bzpopmin(self, r): + r.delete('a') + r.delete('b') + r.delete('c') + r.zadd('a', a1=1, a2=2) + r.zadd('b', b1=10, b2=20) + assert r.bzpopmin(['b', 'a'], timeout=1) == (b('b'), b('b1'), b('10')) + assert r.bzpopmin(['b', 'a'], timeout=1) == (b('b'), b('b2'), b('20')) + assert r.bzpopmin(['b', 'a'], timeout=1) == (b('a'), b('a1'), b('1')) + assert r.bzpopmin(['b', 'a'], timeout=1) == (b('a'), b('a2'), b('2')) + assert r.bzpopmin(['b', 'a'], timeout=1) is None + r.zadd('c', c1=100) + assert r.bzpopmin('c', timeout=1) == (b('c'), b('c1'), b('100')) + def test_zrange(self, r): r.zadd('a', a1=1, a2=2, a3=3) assert r.zrange('a', 0, 1) == [b('a1'), b('a2')] |
