Last active
August 29, 2015 14:10
-
-
Save mritzco/21380727b548e1a71dc1 to your computer and use it in GitHub Desktop.
Implements a REDIS Sorted Set, sorted by order of insertion. Return index (base 1) or 0 if element already exists
This file contains hidden or 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 res = redis.call("zrange", KEYS[1], -1, -1, 'WITHSCORES') | |
local last_id | |
if res then | |
last_id = 1 | |
else | |
last_id = res[2] + 1 | |
end | |
local add_res = redis.call("ZADD", KEYS[1], last_id, ARGV[1]) | |
if add_res == 0 then | |
last_id = 0 | |
end | |
return last_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rev 2. Avoid issues with removed elements before more inserts are done
Revision 1 uses cardinality (length of set)
Revision 2 uses last index+1