Skip to content

Commit

Permalink
Add mexists method that allow to check the existance
Browse files Browse the repository at this point in the history
of multiple keys

It will return a Fixnum indicating how many keys exists
  • Loading branch information
GustavoCaso committed Sep 19, 2019
1 parent 78bf940 commit b0528a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit b0528a4

Please sign in to comment.