Created
February 25, 2011 16:02
-
-
Save catwell/844000 to your computer and use it in GitHub Desktop.
Redis: WATCH use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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