国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:67日期: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;//設(shè)置默認(rèn)值,從環(huán)境變量取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()轉(zhuǎn)為字符串格式即可

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; //設(shè)置默認(rèn)值,從環(huán)境變量取 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()即可調(diào)用

4、多次重復(fù)調(diào)用出現(xiàn)一樣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的實現(xiàn)方法

修改如下

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();};

單個調(diào)用 guid()

n個調(diào)用 guid(n)

文檔

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

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 久久久国产成人精品 | 黄在线观看网站 | 国产2页 | 老色歌uuu26| 综合色久七七综合七七蜜芽 | 国产精品久久精品 | 国内自拍一区 | 67194欧美成l人在线观看免费 | 永久免费看毛片 | 亚洲精品国产三级在线观看 | 国产亚洲精品久久麻豆 | 成人免费视频播放 | 亚洲国产成人精品一区91 | 日韩美女一区 | 日韩在线观看不卡 | 免费在线一区二区三区 | 男女视频在线免费观看 | 亚洲综合网在线观看首页 | 亚洲在线视频免费 | 99久久国产综合精品成人影院 | 国产精品日韩欧美一区二区三区 | 亚洲欧美一区二区三区综合 | xxxxx日本59| 欧美另类专区 | 欧美最猛性xxxxx亚洲精品 | 日韩一级a毛片欧美一级 | 一级做a爰片性色毛片男 | 国产精品露脸脏话对白 | 精品成人 | 怡红院日本一道日本久久 | 国产精品视频免费 | 久久久精品免费观看 | 免费人成年短视频在线观看网站 | 黄色一级毛片免费 | 精品视频一区二区三三区四区 | 国产免费久久精品99 | 欧美精品99| 国产精品免费观看视频播放 | 欧美国产成人在线 | 正在播放的国产a一片 | 亚洲精品日韩一区二区 |