Sha256: c0bcde4e6ba3f08c5de3c79416f28d409e23a66f7d20fbc7ee53d2cdb471b2bd
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
class MemcacheLock class Error < RuntimeError; end DEFAULT_RETRY = 5 DEFAULT_EXPIRY = 30 def initialize(cache) @cache = cache end def synchronize(key, lock_expiry = DEFAULT_EXPIRY, retries = DEFAULT_RETRY) if recursive_lock?(key) yield else acquire_lock(key, lock_expiry, retries) begin yield ensure release_lock(key) end end end def acquire_lock(key, lock_expiry = DEFAULT_EXPIRY, retries = DEFAULT_RETRY) retries.times do |count| begin response = @cache.write("lock/#{key}", Process.pid, :expires_in => lock_expiry, :unless_exist => true) return if response raise Error if count == retries - 1 end exponential_sleep(count) unless count == retries - 1 end raise Error, "Couldn't acquire memcache lock for: #{key}" end def release_lock(key) @cache.delete("lock/#{key}") end def exponential_sleep(count) @runtime += Benchmark::measure { sleep((2**count) / 2.0) } end private def recursive_lock?(key) @cache.read("lock/#{key}") == Process.pid end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
smira-memcache-lock-0.1.0 | lib/memcache-lock.rb |