Skip to content

Instantly share code, notes, and snippets.

@adamwiggins
Created June 20, 2010 21:33
Show Gist options
  • Save adamwiggins/446134 to your computer and use it in GitHub Desktop.
Save adamwiggins/446134 to your computer and use it in GitHub Desktop.
require 'memcache'
CACHE = Memcache.new(:servers => [ 'localhost:11211' ], :namespace => 'test')
def lock(name, secs=60, &block)
return !! CACHE.add("lock:#{name}", '1', secs)
end
def unlock(name)
CACHE.delete("lock:#{name}")
end
if lock('tryit', 5)
puts 'running...'
loop do
puts '.'
sleep 1
lock('tryit', 5)
end
else
puts "couldn't get lock"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment