Skip to content

Instantly share code, notes, and snippets.

@mritzco
Last active August 29, 2015 14:10
Show Gist options
  • Save mritzco/21380727b548e1a71dc1 to your computer and use it in GitHub Desktop.
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
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
@mritzco
Copy link
Author

mritzco commented Dec 2, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment