Sha256: 0412379e0244956c1b98300552594014522094ac5c61fbf7e87fc7cd7e040828
Contents?: true
Size: 1 KB
Versions: 3
Compression:
Stored size: 1 KB
Contents
module CacheAdvance class Lock class LockAcquisitionFailureException < Exception; end DEFAULT_RETRIES = 5 DEFAULT_EXPIRATION_TIME = 30 def initialize(store) @store = store end def execute_locked(key, lock_expiry = DEFAULT_EXPIRATION_TIME, retries = DEFAULT_RETRIES) begin acquire(key, lock_expiry, retries) yield ensure release_lock(key) end end def acquire(key, lock_expiry = DEFAULT_EXPIRATION_TIME, retries = DEFAULT_RETRIES) retries.times do |count| begin return if @store.set("lock/#{key}", Process.pid, lock_expiry) == "STORED\r\n" end exponential_sleep(count) unless count == retries - 1 end raise LockAcquisitionFailureException, "Couldn't acquire memcache lock for: #{key}" end def release_lock(key) @store.delete("lock/#{key}") end def exponential_sleep(count) @runtime += Benchmark::measure { sleep((2**count) / 10.0) } end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
aub-cache_advance-0.2.0 | lib/cache_advance/lock.rb |
aub-cache_advance-1.1.0 | lib/cache_advance/lock.rb |
aub-cache_advance-1.1.1 | lib/cache_advance/lock.rb |