Sha256: 7b3004fe21a6acfcf6b9e288d8166da07d3663821332db4f529658d8698122ec

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Restruct
  class Connection

    def initialize(*args)
      @args = args
      @redis = Redic.new *args
      @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

    def read
      redis.client.read
    end

    def clone
      Connection.new *@args
    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

1 entries across 1 versions & 1 rubygems

Version Path
restruct-0.2.0 lib/restruct/connection.rb