Created
November 3, 2012 20:21
-
-
Save paddycarver/4008599 to your computer and use it in GitHub Desktop.
Concurrency-safe averages calculation and storage for Redis using a script
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
local avg = tonumber(redis.call("get", "avg")) if avg == nil then return { err = "avg not found" } end local datapoints = tonumber(redis.call("get", "datapoints")) if datapoints == nil then return { err = "datapoints not found" } end local raw = avg * datapoints datapoints = datapoints + 1 raw = raw + ARGV[1] local newavg = raw / datapoints redis.call("set", "avg", newavg) redis.call("set", "datapoints", datapoints) return tostring(newavg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment