Skip to content

Commit

Permalink
Implement variadic exists? to replace exists as the boolean method
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jun 9, 2020
1 parent 87df483 commit bf42fc9
Show file tree
Hide file tree
Showing 3 changed files with 27 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 @@ -560,6 +560,16 @@ def exists(key)
end
end

# Determine if any of the keys exists.
#
# @param [String, Array<String>] keys
# @return [Boolean]
def exists?(*keys)
synchronize do |client|
client.call([:exists, *keys], &Boolify)
end
end

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

# Determine if any of the keys exists.
def exists?(*args)
keys_per_node = args.group_by { |key| node_for(key) }
keys_per_node.each do |node, keys|
return true if node.exists?(*keys)
end
false
end

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

def test_exists?
assert_equal false, r.exists?("{1}foo", "{1}bar")

r.set("{1}foo", "s1")

assert_equal true, r.exists?("{1}foo")
end

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

Expand Down

0 comments on commit bf42fc9

Please sign in to comment.