Skip to content

Instantly share code, notes, and snippets.

@dspezia
Created May 3, 2012 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dspezia/2587593 to your computer and use it in GitHub Desktop.
Save dspezia/2587593 to your computer and use it in GitHub Desktop.
Ruby example of pipelining with the distributed client
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