Skip to content

Instantly share code, notes, and snippets.

@paddycarver
Created November 3, 2012 20:21
Show Gist options
  • Save paddycarver/4008599 to your computer and use it in GitHub Desktop.
Save paddycarver/4008599 to your computer and use it in GitHub Desktop.
Concurrency-safe averages calculation and storage for Redis using a script
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