Skip to content

Add support for ZMPOP #1852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import time
import warnings
from typing import List, Optional

from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError

Expand Down Expand Up @@ -3255,6 +3256,25 @@ def bzpopmin(self, keys, timeout=0):
keys.append(timeout)
return self.execute_command("BZPOPMIN", *keys)

def zmpop(
self,
num_keys: int,
keys: List[str],
min_max: str,
count: Optional[int] = 1,
) -> list:
"""
Pop ``count`` values (default 1) off of the first non-empty sorted set
named in the ``keys`` list.

For more information check https://redis.io/commands/zmpop
"""
args = [num_keys] + keys + [min_max]
if count != 1:
args.extend(["COUNT", count])

return self.execute_command("ZMPOP", *args)

def _zrange(
self,
command,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,17 @@ def test_bzpopmin(self, r):
r.zadd("c", {"c1": 100})
assert r.bzpopmin("c", timeout=1) == (b"c", b"c1", 100)

@pytest.mark.onlynoncluster
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
def test_zmpop(self, unstable_r):
unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
res = [b"a", [[b"a1", b"1"], [b"a2", b"2"]]]
assert unstable_r.zmpop("2", ["b", "a"], "MIN", count=2) == res
with pytest.raises(TypeError):
unstable_r.zmpop("2", ["b", "a"], count=2)
unstable_r.zadd("b", {"b1": 10, "ab": 9, "b3": 8})
assert unstable_r.zmpop("2", ["b", "a"], "MAX") == [b"b", [[b"b1", b"10"]]]

def test_zrange(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
assert r.zrange("a", 0, 1) == [b"a1", b"a2"]
Expand Down