class Osbourne::Locks::Redis

Attributes

client[RW]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method Osbourne::Locks::Base::new
# File lib/osbourne/locks/redis.rb, line 10
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/osbourne/locks/redis.rb, line 21
def lock(key, ttl)
  with_pool do |client|
    client.set(key, (Time.current + ttl).to_i, ex: ttl, nx: true)
  end
end
lock!(key, ttl) click to toggle source
# File lib/osbourne/locks/redis.rb, line 27
def lock!(key, ttl)
  with_pool do |client|
    client.set(key, (Time.current + ttl).to_i, ex: ttl)
  end
end
unlock!(key) click to toggle source
# File lib/osbourne/locks/redis.rb, line 33
def unlock!(key)
  with_pool do |client|
    client.del(key)
  end
end

Private Instance Methods

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