class Potlock::Client

Attributes

key[RW]
lock_key[RW]

Public Class Methods

new(key:) click to toggle source
# File lib/potlock/client.rb, line 7
def initialize(key:)
  @key = key
  @lock_key = "#{key}_lock"
end

Public Instance Methods

fetch() { || ... } click to toggle source
# File lib/potlock/client.rb, line 12
def fetch
  lock! do
    return redis.get(key) if redis.exists?(key)

    value = yield
    redis.set(key, value)
    value
  end
rescue Redlock::LockError => _e
  raise Potlock::LockError
end
get() click to toggle source
# File lib/potlock/client.rb, line 24
def get
  lock! { redis.get(key) }
rescue Redlock::LockError => _e
  raise Potlock::LockError
end
set(&block) click to toggle source
# File lib/potlock/client.rb, line 30
def set(&block)
  value = lock!(&block)
  redis.set(key, value)
  value
rescue Redlock::LockError => _e
  raise Potlock::LockError
end

Private Instance Methods

lock!(&block) click to toggle source
# File lib/potlock/client.rb, line 40
def lock!(&block)
  lock_manager.lock!(lock_key, retry_delay, &block)
end
lock_manager() click to toggle source
# File lib/potlock/client.rb, line 44
def lock_manager
  @lock_manager ||= Redlock::Client.new(
    [
      redis,
    ],
    {
      retry_count: Potlock.configuration.retry_count,
      retry_delay: Potlock.configuration.retry_delay,
    },
  )
end
redis() click to toggle source
# File lib/potlock/client.rb, line 60
def redis
  @redis ||= Redis.new(
    host: Potlock.configuration.redis_host,
    db: Potlock.configuration.redis_db,
    port: Potlock.configuration.redis_port,
  )
end
retry_delay() click to toggle source
# File lib/potlock/client.rb, line 56
def retry_delay
  Potlock.configuration.retry_delay * Potlock.configuration.retry_count
end