Skip to content

Instantly share code, notes, and snippets.

@dahernan
Created March 17, 2015 11:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dahernan/fd8d6be29f5655a39989 to your computer and use it in GitHub Desktop.
Save dahernan/fd8d6be29f5655a39989 to your computer and use it in GitHub Desktop.
LUA Redis Snowflake
$ 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