Skip to content

Commit b541da6

Browse files
authored
Add support for SINTERCARD (#1859)
* add sintercard * fix pr comment
1 parent 7ea1bf7 commit b541da6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: redis/commands/core.py

+13
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,19 @@ def sinter(self, keys, *args):
23882388
args = list_or_args(keys, args)
23892389
return self.execute_command("SINTER", *args)
23902390

2391+
def sintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int:
2392+
"""
2393+
Return the cardinality of the intersect of multiple sets specified by ``keys`.
2394+
2395+
When LIMIT provided (defaults to 0 and means unlimited), if the intersection
2396+
cardinality reaches limit partway through the computation, the algorithm will
2397+
exit and yield limit as the cardinality
2398+
2399+
For more information check https://redis.io/commands/sintercard
2400+
"""
2401+
args = [numkeys, *keys, "LIMIT", limit]
2402+
return self.execute_command("SINTERCARD", *args)
2403+
23912404
def sinterstore(self, dest, keys, *args):
23922405
"""
23932406
Store the intersection of sets specified by ``keys`` into a new

Diff for: tests/test_commands.py

+9
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,15 @@ def test_sinter(self, r):
17481748
r.sadd("b", "2", "3")
17491749
assert r.sinter("a", "b") == {b"2", b"3"}
17501750

1751+
@pytest.mark.onlynoncluster
1752+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
1753+
def test_sintercard(self, unstable_r):
1754+
unstable_r.sadd("a", 1, 2, 3)
1755+
unstable_r.sadd("b", 1, 2, 3)
1756+
unstable_r.sadd("c", 1, 3, 4)
1757+
assert unstable_r.sintercard(3, ["a", "b", "c"]) == 2
1758+
assert unstable_r.sintercard(3, ["a", "b", "c"], limit=1) == 1
1759+
17511760
@pytest.mark.onlynoncluster
17521761
def test_sinterstore(self, r):
17531762
r.sadd("a", "1", "2", "3")

0 commit comments

Comments
 (0)