Class: ActiveSupport::Cache::MemCacheStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::MemCacheStore
- Defined in:
- lib/extensions/lock.rb
Instance Method Summary collapse
Instance Method Details
#lock(key, lock_expiry = 30, retries = 5) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/extensions/lock.rb', line 6 def lock(key, lock_expiry = 30, retries = 5) retries.times do |count| begin response = @data.add("lock:#{key}", "Locked by #{Process.pid}", lock_expiry) response ||= Response::STORED rescue Object => e end if response == Response::STORED begin value = yield( @data.get(key) ) @data.set(key, value) return value ensure @data.delete("lock:#{key}") end else sleep((2**count) / 2.0) end end end |