Skip to content

Instantly share code, notes, and snippets.

@onyxraven
Created July 30, 2014 20:28
Show Gist options
  • Save onyxraven/551a04b66606b220e36e to your computer and use it in GitHub Desktop.
Save onyxraven/551a04b66606b220e36e to your computer and use it in GitHub Desktop.
Lua/Redis round-robin increment set
local numbers_list = redis.call('ZRANGE', KEYS[1], 0, -1, 'WITHSCORES')
local numbers = {}
--every other value is a key
for idx = 1, #numbers_list, 2 do
numbers[numbers_list[idx]] = numbers_list[idx + 1]
end
--add missing numbers at '0' and immediately return first one
for _, f in pairs(ARGV) do
if numbers[f] == nil then
redis.call('ZADD', KEYS[1], 1, f)
return { f, 1 }
end
end
--increment lowest
local count = redis.call('ZINCRBY', KEYS[1], 1, numbers_list[1])
return { numbers_list[1], count }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment