Skip to content

Instantly share code, notes, and snippets.

@j-wilkins
Created September 17, 2012 08:45
Show Gist options
  • Save j-wilkins/3736229 to your computer and use it in GitHub Desktop.
Save j-wilkins/3736229 to your computer and use it in GitHub Desktop.
trivial Redis Lua scripts
r = Redis::Namespace.new(:resque, redis: Redis.new)
# the 'correct' way. Redis wants us to specify the keys we'll be operating
# on in the script as an argument to the eval command.
script = <<-LUA
local t = {}
for iter,value in ipairs(KEYS) do
table.insert(t, {value, redis.call('llen', value)})
end
return t
LUA
Hash[r.eval(script, r.keys("batch:*"))] #=> {'batch:<id>' => <count>,...}
# the way where Lua does everything.
# The specifying keys thing is not enforced for now, so we can get
# away with not doing it.
script2 = <<-LUA
local glob = ARGV[1] or "batch:*"
local t = {}
local keys = redis.call('keys', glob)
for iter,value in ipairs(keys) do
table.insert(t, {value, redis.call('llen', value)})
end
return t
LUA
Hash[r.eval(script2, [])] #=> {'batch:<id>' => <count>,...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment