Created
July 30, 2014 20:28
-
-
Save onyxraven/551a04b66606b220e36e to your computer and use it in GitHub Desktop.
Lua/Redis round-robin increment set
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 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