From f4e82e50150950bb279d1f3a96898a08c1ac8205 Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 26 Jan 2017 01:03:42 +0000 Subject: [PATCH 1/2] Add support for MGET to Redis::Distributed. When a distributed redis receives mget, find the nodes the keys should be stored on issue MGET commands to the sub-nodes with only their matching keys, then combine the results and return them in an order that matches the key list. --- lib/redis/distributed.rb | 12 +++++++++++- test/distributed_commands_on_strings_test.rb | 8 +++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index df4914831..6b6952ea5 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -278,8 +278,18 @@ def get(key) end # Get the values of all the given keys. + # def mget(*keys) - raise CannotDistribute, :mget + m_nodes = keys.group_by { |key| node_for(key) } + + results = Hash[ + m_nodes.flat_map do |node, key_list| + values = node.mget(key_list) + key_list.zip(values) + end + ] + + keys.map { |key| results[key] } end def mapped_mget(*keys) diff --git a/test/distributed_commands_on_strings_test.rb b/test/distributed_commands_on_strings_test.rb index ad83c12e5..9f4ed58d6 100644 --- a/test/distributed_commands_on_strings_test.rb +++ b/test/distributed_commands_on_strings_test.rb @@ -9,9 +9,11 @@ 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 From 197d11b046f66237119c78969a4030db54e4fd2b Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 26 Jan 2017 13:57:03 +0000 Subject: [PATCH 2/2] splat the keys --- lib/redis/distributed.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index 6b6952ea5..7705bbe70 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -284,7 +284,7 @@ def mget(*keys) results = Hash[ m_nodes.flat_map do |node, key_list| - values = node.mget(key_list) + values = node.mget(*key_list) key_list.zip(values) end ]