Module: Redis::Lockers

Defined in:
lib/redis/lockers.rb,
lib/redis/lockers/lock.rb,
lib/redis/lockers/version.rb

Overview

Distributed locks with Redis.

Defined Under Namespace

Classes: Lock

Constant Summary collapse

VERSION =

Gem version.

"1.0.0"

Class Method Summary collapse

Class Method Details

.acquire(redis, *args, **kwargs) { ... } ⇒ Object

Creates new lock and yields control if it was successfully acquired. Ensures lock is released after execution of the block.

Examples:


REDIS = Redis.new
Redis::Lockers.acquire(REDIS, :resource_name, :ttl => 60_000) do
  # lock was successfully acquired - we're good to run our code
end

Yields:

  • Executes given block if lock was acquired.

Returns:

  • (Object)

    result of the last expression in the block



22
23
24
25
26
27
# File 'lib/redis/lockers.rb', line 22

def self.acquire(redis, *args, **kwargs)
  lock = Lock.new(*args, **kwargs)
  yield if lock.acquire(redis)
ensure
  lock&.release(redis)
end