Ruby gem for easily pipelining REDIS commands
(Inspired by this blog post on 5-10x Speed Ups by Pipeling Multiple REDIS Commands in Ruby by Dan Adams.)
Pipelining in REDIS is a great way to stay performant when executing multiple commands. It should also be easy to use.
Basic usage involves:
- Enqueueing a number of REDIS commands inside a
- Doing something with the results either afterwards or inside blocks specific to each command.
Ex: (a bit contrived...)
# Put a bunch of values in a few different hashes redis = Redis.connect redis.hset "h1", "foo", 1 redis.hset "h2", "bar", 2 redis.hest "h3", "baz", 3 # Get the values pipelined and sum them up values = RedisPipeliner.pipeline redis do |p| # This would normally be 3 round-trips p << redis.hget("h1", "foo") p << redis.hget("h2", "bar") p << redis.hget("h3", "baz") end values.map(&:to_i).inject(&:+).should == 6
You can also pass in a block to be called for each value rather than operating on the values afterwards:
results =  RedisPipeliner.pipeline redis do |p| [%w(h1 foo), %w(h2 bar), %w(h3 baz)].each do |pair| p << redis.hget(pair, pair) do |value| # referencing pair inside the block results << pair + value end end end results.first.should == "foo1"
See the specs for executable examples.