class Restruct::Connection

Attributes

redis[R]
scripts[R]

Public Class Methods

new(redis=nil) click to toggle source
# File lib/restruct/connection.rb, line 19
def initialize(redis=nil)
  @redis = redis || Redic.new
  @scripts = {}
  @nesting = ::Hash.new { |h,k| h[k] = 0 }
end
simple(*args) click to toggle source
# File lib/restruct/connection.rb, line 7
def simple(*args)
  new Redic.new(*args)
end
with_sentinels(*args) click to toggle source
# File lib/restruct/connection.rb, line 11
def with_sentinels(*args)
  new Redic::Sentinels.new(*args)
end

Public Instance Methods

batch() { || ... } click to toggle source
# File lib/restruct/connection.rb, line 49
def batch
  incr_nesting
  begin
    result = yield
  ensure
    decr_nesting
  end
  commit unless nested?
rescue => ex   
  redis.clear unless nested?
  raise ex
end
call(*args) click to toggle source
# File lib/restruct/connection.rb, line 25
def call(*args)
  raise ArgumentError if args.empty?
  redis.call! *args
rescue RuntimeError => ex
  raise ConnectionErrorFactory.create(ex)
end
lazy(*args) click to toggle source
# File lib/restruct/connection.rb, line 32
def lazy(*args)
  if nested?
    redis.queue *args
    nil
  else
    call *args
  end
end
script(lua_src, *args) click to toggle source
# File lib/restruct/connection.rb, line 41
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

Private Instance Methods

commit() click to toggle source
# File lib/restruct/connection.rb, line 78
def commit
  results = redis.commit 
  error = results.detect { |r| r === RuntimeError }
  raise ConnectionErrorFactory.create(error) if error
  results
end
decr_nesting() click to toggle source
# File lib/restruct/connection.rb, line 74
def decr_nesting
  @nesting[Thread.current.object_id] -= 1
end
incr_nesting() click to toggle source
# File lib/restruct/connection.rb, line 70
def incr_nesting
  @nesting[Thread.current.object_id] += 1
end
nested?() click to toggle source
# File lib/restruct/connection.rb, line 66
def nested?
  @nesting[Thread.current.object_id] > 0
end