Skip to content

Commit 5224ad4

Browse files
authored
Merge pull request #687 from jeremy/distributed-mget
Distributed: support mget and mapped_mget
2 parents 4367cdd + 2e46039 commit 5224ad4

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/redis/distributed.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,16 @@ def get(key)
277277
node_for(key).get(key)
278278
end
279279

280-
# Get the values of all the given keys.
280+
# Get the values of all the given keys as an Array.
281281
def mget(*keys)
282-
raise CannotDistribute, :mget
282+
mapped_mget(*keys).values_at(*keys)
283283
end
284284

285+
# Get the values of all the given keys as a Hash.
285286
def mapped_mget(*keys)
286-
raise CannotDistribute, :mapped_mget
287+
keys.group_by { |k| node_for k }.inject({}) do |results, (node, subkeys)|
288+
results.merge! node.mapped_mget(*subkeys)
289+
end
287290
end
288291

289292
# Overwrite part of a string at key starting at the specified offset.

test/distributed_commands_on_strings_test.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@ class TestDistributedCommandsOnStrings < Test::Unit::TestCase
77
include Lint::Strings
88

99
def test_mget
10-
assert_raise Redis::Distributed::CannotDistribute do
11-
r.mget("foo", "bar")
12-
end
10+
r.set("foo", "s1")
11+
r.set("bar", "s2")
12+
13+
assert_equal ["s1", "s2"] , r.mget("foo", "bar")
14+
assert_equal ["s1", "s2", nil], r.mget("foo", "bar", "baz")
1315
end
1416

1517
def test_mget_mapped
16-
assert_raise Redis::Distributed::CannotDistribute do
17-
r.mapped_mget("foo", "bar")
18-
end
18+
r.set("foo", "s1")
19+
r.set("bar", "s2")
20+
21+
response = r.mapped_mget("foo", "bar")
22+
23+
assert_equal "s1", response["foo"]
24+
assert_equal "s2", response["bar"]
25+
26+
response = r.mapped_mget("foo", "bar", "baz")
27+
28+
assert_equal "s1", response["foo"]
29+
assert_equal "s2", response["bar"]
30+
assert_equal nil , response["baz"]
1931
end
2032

2133
def test_mset

0 commit comments

Comments
 (0)