Class: Redlock::Client::RedisInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/redlock/client.rb,
lib/redlock/testing.rb

Defined Under Namespace

Modules: ConnectionPoolLike

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ RedisInstance

Returns a new instance of RedisInstance.



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/redlock/client.rb', line 156

def initialize(connection)
  if connection.respond_to?(:with)
    @redis = connection
  else
    if connection.respond_to?(:client)
      @redis = connection
    else
      @redis = Redis.new(connection)
    end
    @redis.extend(ConnectionPoolLike)
  end
end

Instance Method Details

#get_remaining_ttl(resource) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/redlock/client.rb', line 183

def get_remaining_ttl(resource)
  recover_from_script_flush do
    @redis.with { |conn| conn.evalsha Scripts::PTTL_SCRIPT_SHA, keys: [resource] }
  end
rescue Redis::BaseConnectionError
  nil
end

#load_scriptsObject



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/redlock/client.rb', line 193

def load_scripts
  scripts = [
    Scripts::UNLOCK_SCRIPT,
    Scripts::LOCK_SCRIPT,
    Scripts::PTTL_SCRIPT
  ]

  scripts.each do |script|
    @redis.with { |conn| conn.script(:load, script) }
  end
end

#load_scripts_without_testingObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redlock/testing.rb', line 40

def load_scripts
  scripts = [
    Scripts::UNLOCK_SCRIPT,
    Scripts::LOCK_SCRIPT,
    Scripts::PTTL_SCRIPT
  ]

  scripts.each do |script|
    @redis.with { |conn| conn.script(:load, script) }
  end
end

#lock(resource, val, ttl, allow_new_lock) ⇒ Object



169
170
171
172
173
# File 'lib/redlock/client.rb', line 169

def lock(resource, val, ttl, allow_new_lock)
  recover_from_script_flush do
    @redis.with { |conn| conn.evalsha Scripts::LOCK_SCRIPT_SHA, keys: [resource], argv: [val, ttl, allow_new_lock] }
  end
end

#unlock(resource, val) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/redlock/client.rb', line 175

def unlock(resource, val)
  recover_from_script_flush do
    @redis.with { |conn| conn.evalsha Scripts::UNLOCK_SCRIPT_SHA, keys: [resource], argv: [val] }
  end
rescue
  # Nothing to do, unlocking is just a best-effort attempt.
end