Skip to content

Instantly share code, notes, and snippets.

@kchristidis
Forked from lloydzhou/redis-aggregate.lua
Created October 9, 2020 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kchristidis/3a4c3db7cccfc7607d30bf20b0fcd17d to your computer and use it in GitHub Desktop.
Save kchristidis/3a4c3db7cccfc7607d30bf20b0fcd17d to your computer and use it in GitHub Desktop.
Aggregate values in a redis sorted set. Returns {count, sum, min, max}
local count = 0
local sum = 0
local min = 0
local max = 0
local cursor = "0"
local result = nil
local data = nil
repeat
result = redis.call("zscan", KEYS[1], cursor, "count", 100)
cursor, data = unpack(result)
for i=1, #data, 2 do
local score = tonumber(data[i + 1])
count = count + 1
sum = sum + score
if i == 1 then
min = score
max = score
else
min = math.min(min, score)
max = math.max(max, score)
end
end
until cursor == "0"
return {
count, sum, min, max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment