Created
March 17, 2015 11:58
-
-
Save dahernan/fd8d6be29f5655a39989 to your computer and use it in GitHub Desktop.
LUA Redis Snowflake
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
$ redis-cli set sequence 1 | |
$ redis-cli set node_id 1 | |
-- id.lua | |
local epoch = 1288834974657 | |
local seq = tonumber(redis.call('INCR', 'sequence')) % 4096 | |
local node = tonumber(redis.call('GET', 'node_id')) % 1024 | |
local time = redis.call('TIME') | |
local time41 = ((tonumber(time[1]) * 1000) + (tonumber(time[2]) / 1000)) - epoch | |
return (time41 * (2 ^ 22)) + (node * (2 ^ 12)) + seq | |
$ redis-cli --eval id.lua | |
(integer) 347205555082385408 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment