Class: ActiveRecordCached::RedisMutex

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_cached/redis_mutex.rb

Constant Summary collapse

KEY =
'ActiveRecordCached:RedisMutex'
MAX_HOLD_TIME =
2
LOCK_ACQUIRER =
"return redis.call('setnx', KEYS[1], 1) == 1 and redis.call('expire', KEYS[1], KEYS[2]) and 1 or 0"

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ RedisMutex

Returns a new instance of RedisMutex.



7
8
9
# File 'lib/activerecord_cached/redis_mutex.rb', line 7

def initialize(redis)
  @redis = redis.then(&:itself)
end

Instance Method Details

#synchronize(&block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/activerecord_cached/redis_mutex.rb', line 11

def synchronize(&block)
  acquire_lock
  begin
    block.call
  ensure
    release_lock
  end
end