Skip to content

Instantly share code, notes, and snippets.

@antirez
Created May 1, 2011 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save antirez/950965 to your computer and use it in GitHub Desktop.
Save antirez/950965 to your computer and use it in GitHub Desktop.
# 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
Copy link

doub commented May 2, 2011

Why don't you declare global functions "set" and "get" instead of putting everything in a "redis" function? It would improve error message consistency: you wouldn't have to create a special error message to tell the user the command is unknown, he would get an "attempt to call nil" kind of error.

@leafstorm
Copy link

@antirez: Yeah, but:

  1. That means that errors don't evaluate to false in an if statement.
  2. It's very unlike normal Lua behavior, which would probably confuse people who already know Lua.

I still think that representing Redis errors with Lua errors would be the best solution. 90% of the time, errors in Redis represent exceptional conditions like improperly calling a command, which is exactly what Lua errors are used for. If there was a time where not directly erroring to the user was important, you could local ok, result = pcall(redis, "get", somekey). ok would contain true or false depending on whether it worked or not, and result would contain either the result of the operation or the error message.

The {ok = "OK"} is a pretty good idea though.

@doub: Because then he would have to define a C-level function for every single Redis command, not to mention polluting the global namespace to heck.

@doub
Copy link

doub commented May 2, 2011

@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.

@shirro
Copy link

shirro commented May 3, 2011

@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