Skip to content

Instantly share code, notes, and snippets.

@adrienmo
Created October 10, 2016 11:10
Show Gist options
  • Save adrienmo/da3a2b369646395952f7ad0e19e43123 to your computer and use it in GitHub Desktop.
Save adrienmo/da3a2b369646395952f7ad0e19e43123 to your computer and use it in GitHub Desktop.
-- This script allow you to get an incremented unique integer id from a string
-- EVAL "SCRIPT" 1 key member1 -> return 1
-- EVAL "SCRIPT" 1 key member2 -> return 2
-- EVAL "SCRIPT" 1 key member1 -> return 1
-- EVAL "SCRIPT" 1 key member3 -> return 3
local nb_keys = redis.call('zcard', KEYS[1]);
local score = redis.call('zscore', KEYS[1], ARGV[1]);
if score then
return tonumber(score);
else
redis.call('zadd',KEYS[1], nb_keys + 1, ARGV[1]);
return nb_keys + 1;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment