Sha256: de3e280606834e5ef24efdd2c2cda72045180d015ccdb0fbd699de69dd22c224
Contents?: true
Size: 1.1 KB
Versions: 1
Compression:
Stored size: 1.1 KB
Contents
require "memcache-lock/version" 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.add("lock/#{key}", Process.pid, lock_expiry) return if response == "STORED\r\n" 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.get("lock/#{key}") == Process.pid end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ht-memcache-lock-0.2.0 | lib/memcache-lock.rb |