summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--redis/commands/core.py13
-rw-r--r--tests/test_commands.py9
2 files changed, 22 insertions, 0 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py
index 383c635..5df9345 100644
--- a/redis/commands/core.py
+++ b/redis/commands/core.py
@@ -2388,6 +2388,19 @@ class SetCommands:
args = list_or_args(keys, args)
return self.execute_command("SINTER", *args)
+ def sintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int:
+ """
+ Return the cardinality of the intersect of multiple sets specified by ``keys`.
+
+ When LIMIT provided (defaults to 0 and means unlimited), if the intersection
+ cardinality reaches limit partway through the computation, the algorithm will
+ exit and yield limit as the cardinality
+
+ For more information check https://redis.io/commands/sintercard
+ """
+ args = [numkeys, *keys, "LIMIT", limit]
+ return self.execute_command("SINTERCARD", *args)
+
def sinterstore(self, dest, keys, *args):
"""
Store the intersection of sets specified by ``keys`` into a new
diff --git a/tests/test_commands.py b/tests/test_commands.py
index f4d7fa7..a7135c0 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -1749,6 +1749,15 @@ class TestRedisCommands:
assert r.sinter("a", "b") == {b"2", b"3"}
@pytest.mark.onlynoncluster
+ # @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
+ def test_sintercard(self, unstable_r):
+ unstable_r.sadd("a", 1, 2, 3)
+ unstable_r.sadd("b", 1, 2, 3)
+ unstable_r.sadd("c", 1, 3, 4)
+ assert unstable_r.sintercard(3, ["a", "b", "c"]) == 2
+ assert unstable_r.sintercard(3, ["a", "b", "c"], limit=1) == 1
+
+ @pytest.mark.onlynoncluster
def test_sinterstore(self, r):
r.sadd("a", "1", "2", "3")
assert r.sinterstore("c", "a", "b") == 0