Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Created January 26, 2015 08:48
Show Gist options
  • Select an option

  • Save alexanderscott/45dd8da221a1152fda92 to your computer and use it in GitHub Desktop.

Select an option

Save alexanderscott/45dd8da221a1152fda92 to your computer and use it in GitHub Desktop.
Redis Lua script to sum the provided key values
-- @desc: Sum the provided key values
-- @usage: redis-cli EVAL "$(cat MGETSUM.lua)" N <key1> <key2> ... <keyN>
-- @return: sum
local function MGETSUM(keys)
local sum = 0
for _,key in ipairs(keys) do
local val = redis.call('GET', key) or 0
sum = sum + tonumber(val)
end
return sum
end
--[[ @TEST
redis.call("SET", "test_str1", 1)
redis.call("SET", "test_str2", 2)
redis.call("SET", "test_str3", 3)
local mgetsum = MGETSUM({"test_str1", "test_str2", "test_str3"})
assert(mgetsum == 6)
redis.call("DEL", "test_str1", "test_str2", "test_str3")
local sumOfZero = MGETSUM({"test_empty1", "test_empty2"})
assert(sumOfZero == 0)
--]]
return MGETSUM(KEYS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment