Created
May 3, 2012 17:53
-
-
Save dspezia/2587593 to your computer and use it in GitHub Desktop.
Ruby example of pipelining with the distributed client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
gem 'redis', '3.0.0.rc1' | |
require 'redis' | |
require 'redis/distributed' | |
# Create a client for 3 distributed Redis instances | |
$r = Redis::Distributed.new %w[redis://localhost:6380 redis://localhost:6381 redis://localhost:6382] | |
# Set 1000 items in distributed Redis | |
$r.flushall | |
(1..1000).each{ |x| $r.set x, 1 } | |
# Check distribution of the keys on the nodes | |
$r.ring.nodes.each do |node| | |
puts "Name: #{node.id}" | |
puts "#{node.keys('*').join '/'}" | |
end | |
# Here is an array containing the keys we need to increment | |
myarray = (1..1000) | |
# Pipeline all increment commands (grouped by Redis instance) | |
htmp = Hash.new {|h, k| h[k] = []} | |
myarray.each {|x| htmp[ $r.node_for(x) ] << x } | |
htmp.each_pair { |node,data| node.pipelined{ data.each{ |key| node.incr(key) }}} | |
# Display values of the keys | |
myarray.each { |x| print $r.get(x),"\n" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment