11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/simple_cache_lock/client.rb', line 11
def lock(lock_key, content_cache_key, options = {}, &block)
return cache_store.read content_cache_key if cache_store.exist? content_cache_key
@options = options
is_locked = redlock.lock(lock_key, lock_timeout)
if is_locked == false
Timeout::timeout(wait_timeout) {
loop do
is_locked = redlock.lock(lock_key, wait_lock_timeout)
if is_locked == false
sleep rand
else
break
end
end
}
if cache_store.exist? content_cache_key
redlock.unlock(is_locked) unless is_locked
return cache_store.read content_cache_key
end
end
result = block.call
cache_store.write content_cache_key, result
redlock.unlock(is_locked)
result
rescue Redlock::LockError, Timeout::Error => error
raise SimpleCacheLock::Error, error
end
|