Class: Restruct::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/restruct/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis = nil) ⇒ Connection

Returns a new instance of Connection.



19
20
21
22
23
# 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

Class Method Details

.simple(*args) ⇒ Object



7
8
9
# File 'lib/restruct/connection.rb', line 7

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

.with_sentinels(*args) ⇒ Object



11
12
13
# File 'lib/restruct/connection.rb', line 11

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

Instance Method Details

#batchObject



49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
47
# 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