According to the redis-rb ruby gem docs;
redis.watch("key") do if redis.get("key") == "some value" redis.multi do |multi| multi.set("key", "other value") multi.incr("counter") end else redis.unwatch endend
I have two questions about this;
- Why is the unwatch line required? My thought is that if key == "some value", set "key" to other value and increase counter if "key" has not changed by the time EXEC is called. In my mind, the else statement never gets executed in this scenario. If you run this code without the else/unwatch, nil is returned.
- If redis.watch "key" is entered without a block (redis.watch "key"); Why am I unable to unwatch a specific key? I get (wrong number of arguments (given 1, expected 0))??
Here is a link to the ruby gem https://github.com/redis/redis-rb