极乐门资源网 Design By www.ioogu.com
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); // 值为:31 this.maxDataCenterId = -1n ^ (-1n << this.dataCenterIdBits); // 值为:31 this.sequenceBits = 12n; this.workerIdShift = this.sequenceBits; // 值为:12 this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; // 值为:17 this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 值为:22 this.sequenceMask = -1n ^ (-1n << this.sequenceBits); // 值为:4095 this.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());
修改如下
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 "_blank" href="https://www.npmjs.com/package/snowflake-id" rel="external nofollow" >文档
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无Js Snowflake(雪花算法)生成随机ID的实现方法的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 第五街的士高《印度激情版》3CD [WAV+CUE][2.4G]
- 三国志8重制版哪个武将智力高 三国志8重制版智力武将排行一览
- 三国志8重制版哪个武将好 三国志8重制版武将排行一览
- 三国志8重制版武将图像怎么保存 三国志8重制版武将图像设置方法
- 何方.1990-我不是那种人【林杰唱片】【WAV+CUE】
- 张惠妹.1999-妹力新世纪2CD【丰华】【WAV+CUE】
- 邓丽欣.2006-FANTASY【金牌大风】【WAV+CUE】
- 饭制《黑神话》蜘蛛四妹手办
- 《燕云十六声》回应跑路:年内公测版本完成95%
- 网友发现国内版《双城之战》第二季有删减:亲亲环节没了!
- 邓丽君2024-《漫步人生路》头版限量编号MQA-UHQCD[WAV+CUE]
- SergeProkofievplaysProkofiev[Dutton][FLAC+CUE]
- 永恒英文金曲精选4《TheBestOfEverlastingFavouritesVol.4》[WAV+CUE]
- 群星《国风超有戏 第9期》[320K/MP3][13.63MB]
- 群星《国风超有戏 第9期》[FLAC/分轨][72.56MB]