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

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

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

瀏覽:204日期: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

控制臺(tái)輸出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(雪花算法)生成隨機(jī)ID的實(shí)現(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();};

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

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

文檔

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

標(biāo)簽: JavaScript
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
午夜久久福利| 电影天堂国产精品| 亚洲久久视频| 日韩三区四区| 欧美一区自拍| 久久超碰99| 日韩精品永久网址| 亚洲午夜一级| 国产99亚洲| 91九色精品| 国产精品精品国产一区二区| 超级白嫩亚洲国产第一| 久久精品动漫| 激情六月综合| 日韩在线网址| 精品一区二区男人吃奶| 日韩欧美二区| 视频一区二区国产| 国产精品国码视频| 99热精品久久| 中文另类视频| 日韩精品欧美精品| 久久久久久一区二区| 亚洲一级高清| 日韩激情综合| 久久97久久97精品免视看秋霞| 欧美日韩亚洲在线观看| 综合在线一区| 超级白嫩亚洲国产第一| 中文字幕一区二区三区日韩精品| 欧美精品观看| 99久久久久久中文字幕一区| 日韩精品视频中文字幕| 九色porny丨国产首页在线| 日韩影院免费视频| 国产一区二区色噜噜| 亚洲一区二区三区高清不卡| 久久av综合| 亚洲a在线视频| 国产亚洲一卡2卡3卡4卡新区| 天堂中文在线播放| 日韩在线网址| 激情欧美国产欧美| 国产伦理一区| 亚洲欧美日本视频在线观看| 精品国产精品国产偷麻豆| 亚洲精品电影| 欧美二区视频| 国产成人精品一区二区三区免费| 美女精品网站| 在线手机中文字幕| 欧美日韩a区| 国产伦精品一区二区三区千人斩 | 麻豆精品在线播放| 在线视频观看日韩| 久久这里只有| 青草国产精品| 视频在线在亚洲| 99精品美女| 黄色网一区二区| 7m精品国产导航在线| 一区视频在线| caoporn视频在线| 国产日韩高清一区二区三区在线 | 国产欧美69| 亚洲aa在线| 激情欧美丁香| 日本精品在线中文字幕| 久久影视三级福利片| 日韩中文字幕一区二区高清99| 国产精品呻吟| 日韩午夜黄色| 欧美一级精品| 另类专区亚洲| 成人国产精品久久| 免费一级欧美在线观看视频| 国产丝袜一区| 欧美日一区二区在线观看| 中文字幕一区二区三区日韩精品| 999在线观看精品免费不卡网站| 999久久久91| 91精品久久久久久久久久不卡| 久久uomeier| 日韩在线不卡| 日韩一区二区三区免费| 亚洲综合电影| 国产一二在线播放| 超级白嫩亚洲国产第一| 久草精品视频| 麻豆国产精品视频| 精品精品99| 亚洲综合电影| 久久久精品午夜少妇| 日韩精品影视| 91精品国产经典在线观看| 亚洲精品第一| 日韩激情网站| 911精品国产| 国产精品一区二区三区www| 性一交一乱一区二区洋洋av| 免费观看在线综合| 日本综合视频| 欧美一区影院| 国产精品美女在线观看直播| 欧美日韩午夜电影网| 国产精品尤物| 精品伊人久久| 成人久久一区| 亚洲经典在线| 日韩一二三区在线观看| 欧美日韩一区自拍| 精品久久亚洲| 久久青草久久| 日韩中文字幕1| 欧美日韩一视频区二区| 久久久久久色| 久久精品99国产精品日本| 国产探花一区在线观看| 久久中文字幕导航| 欧美国产小视频| 九九久久婷婷| 日本在线观看不卡视频| 国产伦乱精品| 日韩在线观看一区| 欧美精品国产| 日韩在线看片| 中文一区一区三区免费在线观| 国产日韩精品视频一区二区三区| 福利在线一区| 久久成人亚洲| 久久精品一区二区国产| 99视频+国产日韩欧美| 欧美一区成人| 亚洲www免费| 妖精视频成人观看www| 国产午夜精品一区在线观看| 久久久久久色| 久久一区二区中文字幕| 一本一道久久a久久| 精品一区不卡| 亚洲男女自偷自拍| 美女av一区| 亚洲欧美日韩专区| 精品国产一区二区三区2021| 国产亚洲精品v| 久久永久免费| 亚洲综合婷婷| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 蜜桃视频在线观看一区二区| 国产精品17p| 欧美高清不卡| 精品资源在线| 香蕉久久久久久| 亚洲成a人片| 国产亚洲高清一区| 91精品国产乱码久久久久久久 | 免费一级欧美片在线观看网站| 自由日本语亚洲人高潮| 欧美精品第一区| 视频在线观看一区二区三区| av日韩中文| 国产亚洲一区| 免费高清在线一区| 日韩精品水蜜桃| 欧美韩一区二区| 国产麻豆久久| 国产精品地址| 视频一区欧美日韩| 色天使综合视频| 欧美韩一区二区| 亚洲精品无播放器在线播放| 亚洲高清毛片| 国产v日韩v欧美v| 国产欧美另类| 综合激情五月婷婷| 国产亚洲精品自拍| 亚洲精品.com| 成人亚洲一区二区| 精品一区二区三区亚洲| 国产欧美日韩免费观看| 9色国产精品| 精品国产一区二区三区噜噜噜| 日本亚洲视频| 国产视频亚洲| 亚洲无线一线二线三线区别av| 国产成人a视频高清在线观看| 日韩精品成人| 亚洲毛片一区| 一区在线免费观看| 日韩精品欧美激情一区二区| 三上亚洲一区二区| 国产精品国码视频| 久久国产欧美日韩精品| 亚洲三区欧美一区国产二区| 欧美日韩国产探花| 亚洲成人不卡| 精品国产亚洲一区二区三区| 国产欧美日韩免费观看| 欧美日韩a区| 久久精品系列|