Created
May 1, 2011 22:56
-
-
Save antirez/950965 to your computer and use it in GitHub Desktop.
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
# Conditional decrement. | |
# | |
# Decrement the value of a key only if the current value is greater than | |
# a specified value. | |
require 'rubygems' | |
require 'redis' | |
r = Redis.new | |
cond_decr = <<LUA | |
local value = tonumber(redis('get',KEYS[1])) | |
if value == nil then return {err="Value at key is not integer"} end | |
if value > tonumber(ARGV[1]) | |
then | |
value = value - 1 | |
redis('set',KEYS[1],value) | |
end | |
return value | |
LUA | |
r.set(:x,4) | |
5.times { | |
puts(r.eval(cond_decr,1,:x,0)) | |
} |
@doub People could do something like https://gist.github.com/952677 if they need it. It might be better to leave such things to third party lua libs and keep the redis code simple.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@leafstorm: If you have a single function, you have to do the dispatch inside the called C function anyway. If there are that many commands to bind, use a code generator. And to avoid polluting global namespace you can put it in a (preloaded) module.