当前位置:网站首页>A method of generating non repeated numbers in nodejs
A method of generating non repeated numbers in nodejs
2022-06-24 10:43:00 【easonxie】
background
Recently when I was doing the demand , There is a management interface that needs to pass an unsigned when calling 32 Bit shaping file ID, That is to say 0 ~ 4294967295 Number between , Every time the interface is called, this file ID Can't repeat .
One 、 Database solutions
The more serious thing to do is , Create a database table for the file , Set a self increasing Int Type primary key id, Then every time you synchronize a file , Insert a file into the database table of the file , To generate a file id, Put this id Pass it to the backstage . But it's more troublesome , After all, our goal is to have a non - Duplicate File id, Doing too much for this purpose is not worth the loss .
Two 、 Using time stamps
JS The timestamp of is converted into a number 13 position ,32 The maximum unsigned integer number of bits is 4294967295, There is no way to treat the timestamp as a file directly id To use . You can change your mind . adopt JS Of Data.now() Get the current timestamp , Subtract a specified timestamp , Calculate the direct time interval between the two , Then multiply this time interval by 100, And then add 0-99 The random number .
The number thus generated , Bits and tens are random numbers , Hundreds or more are self incrementing timestamps . It is almost impossible to repeat , If greater accuracy is required , You can put 100 Switch to 1000, And then add 0-999 The random number .
/** * precision precision 100 1000 10000 */ function getNumberUid(precision){ const rawPre = (Date.now() - new Date(1624206802955).getTime()) / precision; const preNumber = Number(rawPre.toFixed()) * precision; const randam = Math.floor(Math.random() * precision); return preNumber + randam; }
3、 ... and 、 expand
The numbers generated in this way still have a very small probability of repetition , But it can basically handle the interface calls of the management side environment , Ensure no repetition . If it is the only one with a very large amount of data Id Generate , You still need to use a database or other methods . For example, you can refer to teacher liaoxuefeng's article 《 Distributed uniqueness ID generator 》
边栏推荐
- 机械臂速成小指南(零):指南主要内容及分析方法
- Record the range of data that MySQL update will lock
- The difference between static link library and dynamic link library
- Six states of threads
- Web project deployment
- Introduction to the use of splice() method
- 牛客-TOP101-BM29
- JMeter interface test tool foundation - badboy tool
- 126. 单词接龙 II BFS
- [IEEE publication] International Conference on natural language processing and information retrieval in 2022 (ecnlpir 2022)
猜你喜欢
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
The difference between the sleep () method and the wait () method of a thread
SQL Server about like operator (including the problem of field data automatically filling in spaces)
4. classification management business development
Thread operation principle
2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)
【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)
图解杂项【防止丢失进行存档用的】
Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
分布式系统你必须了解的点-CAP
随机推荐
js数组求和的5种方法
Resolved: methods with the same name as their class will not be constructors in
24. image mosaic operation
跨域概述,简单积累
Petit guide de construction rapide du bras mécanique (II): application du bras mécanique
Leetcode-1051: height checker
Five methods of JS array summation
用扫描的方法分发书稿校样
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
88.合并有序数组
Record the range of data that MySQL update will lock
Differences among cookies, session, localstorage and sessionstorage
A group of skeletons flying canvas animation JS special effect
Wechat applet rich text picture width height adaptive method introduction (rich text)
126. 单词接龙 II BFS
Detailed explanation of SQL Sever basic data types
Sort out interface performance optimization skills and kill slow code
js中对象合并的4种方式,对象合并的4种方法
Leetcode interview question 16.06: minimum difference