Skip to content

Instantly share code, notes, and snippets.

@ramonsnir
Created January 26, 2016 14:43
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 ramonsnir/d6cba963241c821babde to your computer and use it in GitHub Desktop.
Save ramonsnir/d6cba963241c821babde to your computer and use it in GitHub Desktop.
Simple Lua script for our test
--[[
ARGUMENTS
KEYS:
1 - key
ARGV:
1 - sub-key to increment
--]]
-- unpack data, or empty data
local data = redis.call("get", KEYS[1])
if data == false then
data = {}
else
data = cmsgpack.unpack(data)
end
-- initialize
if type(data[ARGV[1]]) == "nil" then
data[ARGV[1]] = 0
end
-- increment
data[ARGV[1]] = data[ARGV[1]] + 1
-- write data
redis.call("set", KEYS[1], cmsgpack.pack(data))
-- return, for consistency
return cmsgpack.pack(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment