日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

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

瀏覽:206日期: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
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产欧美日韩视频在线 | 欧美亚洲在线日韩| 麻豆精品99| 欧美国产日本| 久久av导航| 国产一区精品福利| 福利一区二区| 国产不卡人人| 久久精品国内一区二区三区水蜜桃| 久久激五月天综合精品| 日韩欧美在线精品| 国产亚洲欧美日韩在线观看一区二区 | 日本午夜精品一区二区三区电影| 精品一区二区三区四区五区| 一区二区精品伦理...| 不卡在线一区二区| 日韩影院精彩在线| 国产麻豆一区二区三区精品视频| 欧美亚洲色图校园春色| 欧美91在线|欧美| 波多野结衣久久精品| 欧美va天堂在线| 日韩一区免费| 日韩中文影院| 蜜臀精品久久久久久蜜臀 | 色婷婷狠狠五月综合天色拍| 日韩视频免费| 国产精品一区二区精品| 日韩欧美1区| 久热精品在线| 在线亚洲人成| 亚洲欧美日本国产| 国产精品不卡| 中文字幕日本一区二区| 日本中文字幕视频一区| 久久婷婷激情| 久久国内精品视频| 9色精品在线| 国产精品精品| 国产日产一区| 在线观看一区| 精品一区免费| 国产一区国产二区国产三区| 亚洲三级视频| 最新国产拍偷乱拍精品| 日韩欧美中文| 国产在线日韩精品| 欧美亚洲综合视频| 久久国产精品久久久久久电车| 蜜桃av在线播放| 久久中文在线| 国产日韩欧美一区二区三区 | 国产美女久久| 天堂精品久久久久| 老牛影视一区二区三区| 激情自拍一区| 久久在线视频免费观看| 日韩国产欧美| 久久男人天堂| 国产精品手机在线播放| 亚洲精品无播放器在线播放| 欧美.日韩.国产.一区.二区| 日韩网站中文字幕| 视频在线在亚洲| 国产精品成人3p一区二区三区| 免费久久99精品国产| 日韩影片在线观看| 国产精品自在| 精品网站999| 麻豆视频在线看| 国产精品久久亚洲不卡| 美女性感视频久久| 99在线精品免费视频九九视| 日韩精品免费视频一区二区三区| 亚洲精品激情| 精品少妇一区| 在线成人动漫av| 国产欧美丝祙| 欧美在线观看视频一区| 亚洲另类视频| 日韩一区二区三区免费| 日韩精品一区二区三区中文在线 | 日韩欧美精品一区二区综合视频| 911亚洲精品| 国产一区二区精品福利地址| 99视频精品全国免费| 亚洲午夜在线| 国产精品3区| 久久亚洲风情| 91欧美在线| 成人自拍av| 国产精品15p| 亚洲精品亚洲人成在线观看| 麻豆视频在线看| 91成人在线| 色吊丝一区二区| **爰片久久毛片| 欧美91视频| 中文字幕人成乱码在线观看| 欧美一区影院| 亚洲香蕉视频| 国产盗摄——sm在线视频| 免费成人在线影院| 91高清一区| 91tv亚洲精品香蕉国产一区| 亚洲18在线| 久久久久91| 国产精品多人| 日本不卡高清视频| 亚洲欧美日韩国产综合精品二区| 精品亚洲精品| 欧美一级二区| 综合干狼人综合首页| 99久久久国产精品美女| 精品三级国产| 欧美极品中文字幕| 石原莉奈在线亚洲二区| 日韩在线免费| 四虎成人av| 精品国产成人| 久久不见久久见国语| 亚洲影视一区二区三区| 久久aⅴ国产紧身牛仔裤| 不卡中文字幕| 免费av一区二区三区四区| 97精品国产| 97精品国产福利一区二区三区| 精品一区二区三区中文字幕 | 日韩在线电影| 日本中文字幕视频一区| 日本在线观看不卡视频| 亚洲综合婷婷| 亚洲精品精选| 亚洲+小说+欧美+激情+另类| 日本亚洲最大的色成网站www | 精品伊人久久| 亚洲黄色免费看| 久久精品二区三区| 一本大道色婷婷在线| a国产在线视频| 999精品一区| 久久99伊人| 国产欧美一区二区精品久久久 | 日本不卡在线视频| 91精品视频一区二区| 国产伦理一区| 国产videos久久| 久久婷婷丁香| 亚洲精品国产日韩| 久久av国产紧身裤| 久久久久久久久久久9不雅视频| 影院欧美亚洲| 国产精品一区二区中文字幕| 黄毛片在线观看| 人人爽香蕉精品| 麻豆成人av在线| 中文国产一区| 精品一区二区三区在线观看视频| 激情欧美一区| 国产精品v日韩精品v欧美精品网站| 日本在线高清| 亚洲综合中文| 高清精品久久| 日本免费在线视频不卡一不卡二| 国产日产精品一区二区三区四区的观看方式| 伊人久久大香线蕉av不卡| 伊人www22综合色| 国产精品亚洲欧美一级在线| 成人午夜国产| 欧美极品中文字幕| 亚洲精品乱码久久久久久蜜桃麻豆| 久久久久久自在自线| 亚洲精品美女91| 蜜桃国内精品久久久久软件9| 国产精品chinese| 美女网站久久| 国产成人精品亚洲日本在线观看| 日韩黄色在线观看| 久久久久国产精品一区二区| 成人国产精品一区二区免费麻豆| 亚洲精品福利| 亚洲一区日韩| 国产精品av久久久久久麻豆网| 精品国产欧美日韩| 国产精品久久久免费| 91欧美极品| 四虎在线精品| 日韩精品一区第一页| 中文一区二区| 欧美日韩免费观看一区=区三区| 欧美一级鲁丝片| 精品国产成人| 欧美国产三级| 国产精品视频一区二区三区四蜜臂| 亚洲精品大片| 中文字幕av亚洲精品一部二部| 99国产成+人+综合+亚洲欧美| 99精品电影| 国产综合欧美| 日韩中文字幕91|