Skip to content

Instantly share code, notes, and snippets.

@catwell
Created February 25, 2011 16:02
Show Gist options
  • Save catwell/844000 to your computer and use it in GitHub Desktop.
Save catwell/844000 to your computer and use it in GitHub Desktop.
Redis: WATCH use case
require "redis"
R = Redis.new
def watching(*kk)
5.times do
R::watch(*kk)
begin
r = yield
rescue Object => e
R::unwatch
raise e
end
return r unless r.nil?
sleep 1
end
raise("Error watching #{kk.inspect}")
end
# create user "daniel"
R::hmset("u:23","name","daniel","email","daniel@example.com")
# change schema (u -> users)
watching("u:23") do
n,e = R::hmget("u:23","name","email")
R::multi{ R::hmset("users:daniel","name",n,"email",e) }
end
# "daniel" can change his email in the meantime!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment