class Circuitry::Locks::Redis

Attributes

client[RW]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method Circuitry::Locks::Base::new
# File lib/circuitry/locks/redis.rb, line 6
def initialize(options = {})
  super(options)

  self.client = options.fetch(:client) do
    require 'redis'
    ::Redis.new(options)
  end
end

Protected Instance Methods

lock(key, ttl) click to toggle source
# File lib/circuitry/locks/redis.rb, line 17
def lock(key, ttl)
  with_pool do |client|
    client.set(key, (Time.now + ttl).to_i, ex: ttl, nx: true)
  end
end
lock!(key, ttl) click to toggle source
# File lib/circuitry/locks/redis.rb, line 23
def lock!(key, ttl)
  with_pool do |client|
    client.set(key, (Time.now + ttl).to_i, ex: ttl)
  end
end
unlock!(key) click to toggle source
# File lib/circuitry/locks/redis.rb, line 29
def unlock!(key)
  with_pool do |client|
    client.del(key)
  end
end

Private Instance Methods

pool?() click to toggle source
# File lib/circuitry/locks/redis.rb, line 47
def pool?
  defined?(ConnectionPool) && client.is_a?(ConnectionPool)
end
with_pool() { |client| ... } click to toggle source
# File lib/circuitry/locks/redis.rb, line 39
def with_pool(&block)
  if pool?
    client.with(&block)
  else
    yield client
  end
end