Skip to content
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

Add mexists method that allow to check the existance of multiple keys #873

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@ def exists(key)
end
end

# Count existing keys.
#
# @param [Array<String>] key
# @return [Fixnum]
def mexists(*keys)
synchronize do |client|
client.call([:exists] + keys)
end
end

# Find all keys matching the given pattern.
#
# @param [String] pattern
Expand Down
8 changes: 8 additions & 0 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ def exists(key)
node_for(key).exists(key)
end

# Count existing keys.
def mexists(*args)
keys_per_node = args.group_by { |key| node_for(key) }
keys_per_node.inject(0) do |sum, (node, keys)|
sum + node.mexists(*keys)
end
end

# Find all keys matching the given pattern.
def keys(glob = "*")
on_each_node(:keys, glob).flatten
Expand Down
12 changes: 12 additions & 0 deletions test/lint/value_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ def test_exists
assert_equal true, r.exists("foo")
end

def test_mexists
refute r.exists(["foo", "baz"])

r.set("foo", "s1")

assert_equal 1, r.mexists(["foo", "baz"])

r.set("baz", "s2")

assert_equal 2, r.mexists(["foo", "baz"])
end

def test_type
assert_equal "none", r.type("foo")

Expand Down