Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,16 @@ def get(key)
node_for(key).get(key)
end

# Get the values of all the given keys.
# Get the values of all the given keys as an Array.
def mget(*keys)
raise CannotDistribute, :mget
mapped_mget(*keys).values_at(*keys)
end

# Get the values of all the given keys as a Hash.
def mapped_mget(*keys)
raise CannotDistribute, :mapped_mget
keys.group_by { |k| node_for k }.inject({}) do |results, (node, subkeys)|
results.merge! node.mapped_mget(*subkeys)
end
end

# Overwrite part of a string at key starting at the specified offset.
Expand Down
24 changes: 18 additions & 6 deletions test/distributed_commands_on_strings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ class TestDistributedCommandsOnStrings < Test::Unit::TestCase
include Lint::Strings

def test_mget
assert_raise Redis::Distributed::CannotDistribute do
r.mget("foo", "bar")
end
r.set("foo", "s1")
r.set("bar", "s2")

assert_equal ["s1", "s2"] , r.mget("foo", "bar")
assert_equal ["s1", "s2", nil], r.mget("foo", "bar", "baz")
end

def test_mget_mapped
assert_raise Redis::Distributed::CannotDistribute do
r.mapped_mget("foo", "bar")
end
r.set("foo", "s1")
r.set("bar", "s2")

response = r.mapped_mget("foo", "bar")

assert_equal "s1", response["foo"]
assert_equal "s2", response["bar"]

response = r.mapped_mget("foo", "bar", "baz")

assert_equal "s1", response["foo"]
assert_equal "s2", response["bar"]
assert_equal nil , response["baz"]
end

def test_mset
Expand Down