Coder Social home page Coder Social logo

redis_pipeliner's Introduction

Ruby gem for easily pipelining REDIS commands

https://github.com/mrdanadams/redis_pipeliner

(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.

Usage

Basic usage involves:

  1. Enqueueing a number of REDIS commands inside a pipelined block
  2. 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[0], pair[1]) do |value|
      # referencing pair inside the block
      results << pair[1] + value
    end
  end
end
results.first.should == "foo1"

See the specs for executable examples.

githalytics.com alpha

redis_pipeliner's People

Contributors

mrdanadams avatar

Watchers

James Cloos avatar Evan Gray avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.