Sha256: 64d83776c8c7bd88d8b57976003c25738fee70f8450a1b61787597af53f95a4b

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Restruct
  class Connection

    def initialize(*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

    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.1.0 lib/restruct/connection.rb