diff --git a/redis/commands/core.py b/redis/commands/core.py index b06bb5c941..a4a48f95f1 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3153,6 +3153,19 @@ def zinterstore(self, dest, keys, aggregate=None): """ return self._zaggregate("ZINTERSTORE", dest, keys, aggregate) + def zintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int: + """ + Return the cardinality of the intersect of multiple sorted 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/zintercard + """ + args = [numkeys, *keys, "LIMIT", limit] + return self.execute_command("ZINTERCARD", *args) + def zlexcount(self, name, min, max): """ Return the number of items in the sorted set ``name`` between the diff --git a/tests/test_commands.py b/tests/test_commands.py index cdcf1202d8..f4d7fa73e0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1979,6 +1979,15 @@ def test_zinter(self, r): (b"a1", 23), ] + @pytest.mark.onlynoncluster + # @skip_if_server_version_lt("7.0.0") turn on after redis 7 release + def test_zintercard(self, unstable_r): + unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 1}) + unstable_r.zadd("b", {"a1": 2, "a2": 2, "a3": 2}) + unstable_r.zadd("c", {"a1": 6, "a3": 5, "a4": 4}) + assert unstable_r.zintercard(3, ["a", "b", "c"]) == 2 + assert unstable_r.zintercard(3, ["a", "b", "c"], limit=1) == 1 + @pytest.mark.onlynoncluster def test_zinterstore_sum(self, r): r.zadd("a", {"a1": 1, "a2": 1, "a3": 1})