Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
## Output
Node: Keys
6379: ["120"]
6380: ["119", "110", "111", "114", "105", "115", "106", "107", "118"]
6381: ["102", "112", "103", "113", "104", "100", "101"]
6382: ["116", "117", "108", "109"]
First key from each node
["119", "102", "116", "120"]
# before patch
mget(key_from_each_node) => [nil, nil, nil, "120"]
mget(*key_from_each_node) => ["119", nil, nil, nil]
# after patch
mget(key_from_each_node) => ["119", "120", "116", "102"]
mget(*key_from_each_node) => ["119", "120", "116", "102"]
require 'dist_redis'
Redis.module_eval { attr_accessor :port }
dr = DistRedis.new(:hosts => %w(localhost:6379 localhost:6380 localhost:6381 localhost:6382'), :db => 10)
## setup
dr.delete_cloud!
100.upto(120) { |i| dr.set(i, i) }
## capture & print our key distribution
key_distribution = {}
puts "\nNode: Keys"
dr.ring.nodes.each { |node|
port = node.port
keys = node.keys('*')
key_distribution[port] = keys
puts "#{ port }: #{ keys.inspect }"
}
puts "\nFirst key from each node"
key_from_each_node = key_distribution.values.collect(&:first)
p key_from_each_node # => ["119", "102", "116", "120"]
puts "\n# before patch"
print "mget(key_from_each_node) => #{ dr.mget(key_from_each_node).inspect }\n"
print "mget(*key_from_each_node) => #{ dr.mget(*key_from_each_node).inspect }\n"
# improvement??
DistRedis.module_eval do
def mget(*keyz)
results = {}
kbn = keys_by_node(keyz)
kbn.each do |node, node_keyz|
node.mapped_mget(*node_keyz).each do |k, v|
results[k] = v
end
end
keyz.flatten.map { |k| results[k] }
end
private
def keys_by_node(*keyz)
keyz.flatten.inject({}) do |kbn, k|
node = node_for_key(k)
next if kbn[node] && kbn[node].include?(k)
kbn[node] ||= []
kbn[node] << k
kbn
end
end
end
puts "\n# after patch"
print "mget(key_from_each_node) => #{ dr.mget(key_from_each_node).inspect }\n"
print "mget(*key_from_each_node) => #{ dr.mget(*key_from_each_node).inspect }\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment