Created
September 17, 2012 08:45
-
-
Save j-wilkins/3736229 to your computer and use it in GitHub Desktop.
trivial Redis Lua scripts
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
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