Sha256: 54fdc4d81166c28601a3f07e4736d07c0317ff023f0b22b6e0b8175a90523673

Contents?: true

Size: 559 Bytes

Versions: 1

Compression:

Stored size: 559 Bytes

Contents

module SyncMachine
  # General-purpose class for locking via Redis.
  class RedisLock
    def initialize(redis_key)
      @redis_key = redis_key
      @acquired = false
    end

    def acquire(&block)
      yield_and_release(block) if set_redis_key
    end

    def acquired?
      @acquired
    end

    private

    def set_redis_key
      @acquired = Redis.current.set(
        @redis_key, "true", nx: true, ex: 10.minutes
      )
    end

    def yield_and_release(block)
      block.call
    ensure
      Redis.current.del(@redis_key)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sync_machine-1.4.0 lib/sync_machine/redis_lock.rb