Sha256: 6772021a03c978b0319dbd154ca9120edbc4a6f48b10ba98aed0c0acf6cf66a3
Contents?: true
Size: 756 Bytes
Versions: 2
Compression:
Stored size: 756 Bytes
Contents
class MemCache # # Try to acquire a global lock from memcached for a particular key. # If successful, yield and set the key to the return value, then release # the lock. # # Based on http://rubyurl.com/Sw7 , which I partially wrote. # def lock(key, lock_expiry = 30, retries = 5) retries.times do |count| response = CACHE.add("lock:#{key}", "Locked by #{Process.pid}", lock_expiry) if response == "STORED\r\n" begin value = yield(CACHE.get(key)) CACHE.set(key, value) return value ensure CACHE.delete("lock:#{key}") end else sleep((2**count) / 2.0) end end raise MemCacheError, "Couldn't acquire lock for #{key}" end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
interlock-1.0 | lib/interlock/memcached.rb |
interlock-1.1 | lib/interlock/memcached.rb |