久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Js Snowflake(雪花算法)生成隨機ID的實現方法

瀏覽:99日期:2024-04-23 18:31:39

1、snowflake-id插件

import SnowflakeId from 'snowflake-id';const guid = num => { const id= new SnowflakeId(); return id.generate();};

2、原生使用

var Snowflake = /** @class */ (function() {function Snowflake(_workerId, _dataCenterId, _sequence) {this.twepoch = 1288834974657n;//this.twepoch = 0n;this.workerIdBits = 5n;this.dataCenterIdBits = 5n;this.maxWrokerId = -1n ^ (-1n << this.workerIdBits); // 值為:31this.maxDataCenterId = -1n ^ (-1n << this.dataCenterIdBits); // 值為:31this.sequenceBits = 12n;this.workerIdShift = this.sequenceBits; // 值為:12this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; // 值為:17this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 值為:22this.sequenceMask = -1n ^ (-1n << this.sequenceBits); // 值為:4095this.lastTimestamp = -1n;//設置默認值,從環境變量取this.workerId = 1n;this.dataCenterId = 1n;this.sequence = 0n;if(this.workerId > this.maxWrokerId || this.workerId < 0) {thrownew Error(’_workerId must max than 0 and small than maxWrokerId-[’ + this.maxWrokerId + ’]’);}if(this.dataCenterId > this.maxDataCenterId || this.dataCenterId < 0) {thrownew Error(’_dataCenterId must max than 0 and small than maxDataCenterId-[’ + this.maxDataCenterId + ’]’);}this.workerId = BigInt(_workerId);this.dataCenterId = BigInt(_dataCenterId);this.sequence = BigInt(_sequence);}Snowflake.prototype.tilNextMillis = function(lastTimestamp) {var timestamp = this.timeGen();while(timestamp <= lastTimestamp) {timestamp = this.timeGen();}return BigInt(timestamp);};Snowflake.prototype.timeGen = function() {return BigInt(Date.now());};Snowflake.prototype.nextId = function() {var timestamp = this.timeGen();if(timestamp < this.lastTimestamp) {thrownew Error(’Clock moved backwards. Refusing to generate id for ’ +(this.lastTimestamp - timestamp));}if(this.lastTimestamp === timestamp) {this.sequence = (this.sequence + 1n) & this.sequenceMask;if(this.sequence === 0n) {timestamp = this.tilNextMillis(this.lastTimestamp);}} else {this.sequence = 0n;}this.lastTimestamp = timestamp;return((timestamp - this.twepoch) << this.timestampLeftShift) |(this.dataCenterId << this.dataCenterIdShift) |(this.workerId << this.workerIdShift) |this.sequence;};return Snowflake;}());console.log(new Snowflake(1n, 1n, 0n).nextId());//1141531990672150528n

控制臺輸出1141531990672150528n為bigint格式, .toString()轉為字符串格式即可

3、ES6使用

import bigInt from 'big-integer';const guid = () => { const Snowflake = /** @class */ (function() { function Snowflake(_workerId, _dataCenterId, _sequence) { // this.twepoch = 1288834974657; this.twepoch = 0; this.workerIdBits = 5; this.dataCenterIdBits = 5; this.maxWrokerId = -1 ^ (-1 << this.workerIdBits); // 值為:31 this.maxDataCenterId = -1 ^ (-1 << this.dataCenterIdBits); // 值為:31 this.sequenceBits = 12; this.workerIdShift = this.sequenceBits; // 值為:12 this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; // 值為:17 this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 值為:22 this.sequenceMask = -1 ^ (-1 << this.sequenceBits); // 值為:4095 this.lastTimestamp = -1; //設置默認值,從環境變量取 this.workerId = 1; this.dataCenterId = 1; this.sequence = 0; if (this.workerId > this.maxWrokerId || this.workerId < 0) { throw new Error( ’config.worker_id must max than 0 and small than maxWrokerId-[’ + this.maxWrokerId + ’]’ ); } if (this.dataCenterId > this.maxDataCenterId || this.dataCenterId < 0) { throw new Error( ’config.data_center_id must max than 0 and small than maxDataCenterId-[’ + this.maxDataCenterId + ’]’ ); } this.workerId = _workerId; this.dataCenterId = _dataCenterId; this.sequence = _sequence; } Snowflake.prototype.tilNextMillis = function(lastTimestamp) { var timestamp = this.timeGen(); while (timestamp <= lastTimestamp) { timestamp = this.timeGen(); } return timestamp; }; Snowflake.prototype.timeGen = function() { //new Date().getTime() === Date.now() return Date.now(); }; Snowflake.prototype.nextId = function() { var timestamp = this.timeGen(); if (timestamp < this.lastTimestamp) { throw new Error( ’Clock moved backwards. Refusing to generate id for ’ + (this.lastTimestamp - timestamp) ); } if (this.lastTimestamp === timestamp) { this.sequence = (this.sequence + 1) & this.sequenceMask; if (this.sequence === 0) { timestamp = this.tilNextMillis(this.lastTimestamp); } } else { this.sequence = 0; } this.lastTimestamp = timestamp; var shiftNum = (this.dataCenterId << this.dataCenterIdShift) | (this.workerId << this.workerIdShift) | this.sequence; // dataCenterId:1,workerId:1,sequence:0 shiftNum:135168 var nfirst = new bigInt(String(timestamp - this.twepoch), 10); nfirst = nfirst.shiftLeft(this.timestampLeftShift); var nnextId = nfirst.or(new bigInt(String(shiftNum), 10)).toString(10); return nnextId; }; return Snowflake; })(); return new Snowflake(1, 1, 0).nextId();};

guid()即可調用

4、多次重復調用出現一樣id的bug

console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime()); console.log(guid(), new Date().getTime());

Js Snowflake(雪花算法)生成隨機ID的實現方法

修改如下

import SnowflakeId from 'snowflake-id';const guid = num => { const snowflake = new SnowflakeId(); let arr = []; for (let i = 0; i < num; i++) { arr.push(snowflake.generate()); } return num ? arr : snowflake.generate();};

單個調用 guid()

n個調用 guid(n)

文檔

到此這篇關于Js Snowflake(雪花算法)生成隨機ID的實現方法的文章就介紹到這了,更多相關Js 雪花算法生成隨機ID內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 涩涩天堂| 国内成人精品2018免费看 | 九九九色| 欧美激情在线狂野欧美精品 | 日韩欧美国产一区二区 | 伊人在线| 国产精品影院在线观看 | 色135综合网 | 日韩免费一区 | 天天综合7799精品影视 | 91精品国产综合久久婷婷香蕉 | 欧美一级c片 | 日韩一及片 | 黄视频网址| 欧美日韩精品一区二区三区在线观看 | 色欧美日韩 | 亚洲男人的天堂网站 | 精品成人免费一区二区在线播放 | 国产美女高潮一区二区三区 | 中文字幕精品一区久久久久 | 国产日韩欧美在线 | 一区二区三区四区精品 | 久久99久久99精品 | 免费观看电视在线高清视频 | 国产一级纯肉体一级毛片 | 国产大奶视频 | 亚洲欧美中文字幕 | 亚洲一区视频在线播放 | 日韩午夜电影在线观看 | 亚洲高清在线视频 | 欧美精品成人一区二区在线 | 北条麻妃99精品青青久久主播 | 毛片久久| 欧美性一区二区三区 | 久久91久久久久麻豆精品 | 久草资源在线视频 | 欧美日韩精品一区二区三区 | 最新伦理片 | 日韩理伦片在线观看视频播放 | 欧美激情综合五月色丁香小说 | 龙珠z国语版291集全 |