2 Star 4 Fork 1

aaron_hgc/redis-id-generator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
redis-script-node2.lua 946 Bytes
一键复制 编辑 原始数据 按行查看 历史
hengyunabc 提交于 2016-06-17 03:12 . fix repeated id problem #2
-- need redis 3.2+
redis.replicate_commands();
local prefix = '__idgenerator_';
local partitionCount = 4096;
local step = 3;
local startStep = 1;
local tag = KEYS[1];
-- if user do not pass shardId, default partition is 0.
local partition
if KEYS[2] == nil then
partition = 0;
else
partition = KEYS[2] % partitionCount;
end
local now = redis.call('TIME');
local miliSecondKey = prefix .. tag ..'_' .. partition .. '_' .. now[1] .. '_' .. math.floor(now[2]/1000);
local count;
repeat
count = tonumber(redis.call('INCRBY', miliSecondKey, step));
if count > (1024 - step) then
now = redis.call('TIME');
miliSecondKey = prefix .. tag ..'_' .. partition .. '_' .. now[1] .. '_' .. math.floor(now[2]/1000);
end
until count <= (1024 - step)
if count == step then
redis.call('PEXPIRE', miliSecondKey, 5);
end
-- second, microSecond, partition, seq
return {tonumber(now[1]), tonumber(now[2]), partition, count + startStep}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aaron_hgc/redis-id-generator.git
[email protected]:aaron_hgc/redis-id-generator.git
aaron_hgc
redis-id-generator
redis-id-generator
master

搜索帮助