Skip to content

Instantly share code, notes, and snippets.

@Hidendra
Last active August 4, 2022 10:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Hidendra/bd78d26bd6eb498115dd to your computer and use it in GitHub Desktop.
Save Hidendra/bd78d26bd6eb498115dd to your computer and use it in GitHub Desktop.
zadd that keeps track of sums
-- keys: key dest
-- argv: score member [score member ...]
-- e.g. keys: "data:test:1:2" "data-sum:test:1:2"
-- argv: 10 a 15 b
local key = KEYS[1]
local dest = KEYS[2]
local sum = tonumber(redis.call('get', dest)) or 0
for i=1, #ARGV, 2 do
local score = tonumber(ARGV[i]) or 0
local member = ARGV[i + 1]
local currentScore = tonumber(redis.call('zscore', key, member))
if score ~= currentScore then
sum = sum + score - (currentScore or 0)
redis.call('zadd', key, score, member)
end
end
redis.call('set', dest, sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment