Sha256: b6479e0fb96fd115507c8ce8fb39fbad3d565b4d8557975917819c1870ad3440

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

require 'oxblood/protocol'
require 'oxblood/commands'

module Oxblood
  # Redis pipeling class. Commands won't be send until {#sync} is called.
  # Error responses won't be raises and should be checked manually in the
  # responses array.
  # @see http://redis.io/topics/pipelining#redis-pipelining
  #
  # @example Basic workflow
  #   pipeline = Pipeline.new(connection)
  #   pipeline.echo('ping')
  #   pipeline.ping
  #   pipeline.echo('!')
  #   pipeline.sync # => ["ping", "PONG", "!"]
  class Pipeline
    include Commands

    attr_reader :connection

    def initialize(connection)
      @connection = connection
      @commands = Array.new
    end

    # Sends all commands at once and reads responses
    # @return [Array] of responses
    def sync
      serialized_commands = @commands.map { |c| Protocol.build_command(*c) }
      connection.socket.write(serialized_commands.join)
      Array.new(@commands.size) { connection.read_response }
    ensure
      @commands.clear
    end

    private

    def run(*command)
      @commands << command
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
oxblood-0.2.0 lib/oxblood/pipeline.rb
oxblood-0.1.0 lib/oxblood/pipeline.rb
oxblood-0.1.0.dev12 lib/oxblood/pipeline.rb
oxblood-0.1.0.dev11 lib/oxblood/pipeline.rb