diff --git a/lib/redis/commands/sets.rb b/lib/redis/commands/sets.rb index e05751397..fcd538e57 100644 --- a/lib/redis/commands/sets.rb +++ b/lib/redis/commands/sets.rb @@ -154,6 +154,19 @@ def sinterstore(destination, *keys) send_command([:sinterstore, destination].concat(keys)) end + # This command is similar to SINTER, but instead of returning the result set, + # it returns just the cardinality of the result. + # + # @param [String, Array] keys keys pointing to sets to intersect + # @param [Interger] limits number of matches searched for + # @return [Array] cardinality of the intersection + def sintercard(*keys, limit: nil) + args = [:sintercard, keys.size, keys] + args << "LIMIT" << limit if limit + + send_command(args) + end + # Add multiple sets. # # @param [String, Array] keys keys pointing to sets to unify diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index e92d417fa..9c52306bf 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -648,6 +648,15 @@ def sinterstore(destination, *keys) end end + # This command is similar to SINTER, but instead of returning the result set, + # it returns just the cardinality of the result. + def sintercard(*keys, limit: nil) + keys.flatten!(1) + ensure_same_node(:sintercard, keys) do |node| + node.sinter(*keys, limit: limit) + end + end + # Add multiple sets. def sunion(*keys) keys.flatten!(1) diff --git a/test/lint/sets.rb b/test/lint/sets.rb index bfaa5cf84..a361bd7f1 100644 --- a/test/lint/sets.rb +++ b/test/lint/sets.rb @@ -237,6 +237,14 @@ def test_variadic_sinterstore_expand assert_equal 1, r.sinterstore('{1}baz', '{1}foo', '{1}bar') end + def test_sintercard + r.sadd '{1}foo', 's1' + r.sadd '{1}foo', 's2' + r.sadd '{1}bar', 's2' + + assert_equal 1, r.sintercard('{1}foo', '{1}bar') + end + def test_sunion r.sadd 'foo', 's1' r.sadd 'foo', 's2'