Sha256: 13f94449bd28d91632e081b66f4130998dee69e019ebfa66f67900b863734c24

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Restruct

  class Connection

    class << self

      def simple(*args)
        new Redic.new(*args)
      end

      def with_sentinels(*args)
        new Redic::Sentinels.new(*args)
      end

      private :new

    end

    def initialize(redis=nil)
      @redis = redis || Redic.new
      @scripts = {}
      @nesting = ::Hash.new { |h,k| h[k] = 0 }
    end

    def call(*args)
      raise ArgumentError if args.empty?
      redis.call! *args
    rescue RuntimeError => ex
      raise ConnectionErrorFactory.create(ex)
    end

    def lazy(*args)
      if nested?
        redis.queue *args
        nil
      else
        call *args
      end
    end

    def script(lua_src, *args)
      scripts[lua_src] ||= call 'SCRIPT', 'LOAD', lua_src
      call 'EVALSHA', scripts[lua_src], *args
    rescue NoScriptError
      scripts.delete lua_src
      retry
    end

    def batch
      incr_nesting
      begin
        result = yield
      ensure
        decr_nesting
      end
      commit unless nested?
    rescue => ex   
      redis.clear unless nested?
      raise ex
    end

    private 

    attr_reader :redis, :scripts

    def nested?
      @nesting[Thread.current.object_id] > 0
    end

    def incr_nesting
      @nesting[Thread.current.object_id] += 1
    end

    def decr_nesting
      @nesting[Thread.current.object_id] -= 1
    end

    def commit
      results = redis.commit 
      error = results.detect { |r| r === RuntimeError }
      raise ConnectionErrorFactory.create(error) if error
      results
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restruct-1.1.0 lib/restruct/connection.rb
restruct-1.0.0 lib/restruct/connection.rb