Class: MemCache

Inherits:
Object show all
Defined in:
lib/interlock/memcached.rb

Instance Method Summary collapse

Instance Method Details

#lock(key, lock_expiry = 30, retries = 5) ⇒ Object

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 rubyurl.com/Sw7 , which I partially wrote.

Raises:

  • (MemCacheError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/interlock/memcached.rb', line 12

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