请教一个生成唯一id的算法是否存在重复?
在网上看到一段代码 生成唯一id,有人评论说php的这段代码 生成id时重复性比较大,请教一下大家的看法,
class Idwork{ const debug = 1; static $workerId; static $twepoch = 1361775855078; static $sequence = 0; const workerIdBits = 4; static $maxWorkerId = 15; const sequenceBits = 10; static $workerIdShift = 10; static $timestampLeftShift = 14; static $sequenceMask = 1023; private static $lastTimestamp = -1; public function __construct($workId) { if ($workId > self::$maxWorkerId || $workId < 0) { throw new Exception('worker Id can\'t be greater than 15 or less than 0'); } self::$workerId = $workId; } public function timeGen() { //获得当前时间戳 $time = explode(' ', microtime()); $time2 = substr($time[0], 2, 3); $timestramp = $time[1] . $time2; return $time[1] . $time2; } public function tilNextMillis($lastTimestamp) { $timestamp = $this->timeGen(); while ($timestamp <= $lastTimestamp) { $timestamp = $this->timeGen(); } return $timestamp; } public function nextId() { $timestamp = $this->timeGen();//1452043798718 if (self::$lastTimestamp == $timestamp) { self::$sequence = self::$sequence + 1 & self::$sequenceMask; if (self::$sequence == 0) { $timestamp = $this->tilNextMillis(self::$lastTimestamp); } } else { self::$sequence = 0; } if ($timestamp < self::$lastTimestamp) { throw new Excwption('Clock moved backwards. Refusing to generate id for ' . (self::$lastTimestamp - $timestamp) . ' milliseconds'); } self::$lastTimestamp = $timestamp; $nextId = sprintf('%.0f', $timestamp) - sprintf('%.0f', self::$twepoch) | self::$workerId << self::$workerIdShift | self::$sequence; return $nextId; }}$Idwork = new Idwork(1);$a = $Idwork->nextId();
还有下面这个生成唯一id的方式
function get_order_sn(){ mt_srand((double) microtime() * 1000000); return date('Ymd') . str_pad(mt_rand(1, 99999), 4, '0', STR_PAD_LEFT);}echo get_order_sn();
这两种方式哪种更好一些,或者还有没有其它的方式在高并发的情况下生成唯一id不重复的方法
回复讨论(解决方案)
请使用PHP内置函数uniqid
参考: http://php.net/manual/zh/function.uniqid.php
echo uniqid(), PHP_EOL;echo uniqid(), PHP_EOL;
568c83e69c671568c83e69c671
function get_order_sn(){ mt_srand((double) microtime() * 1000000); return date('Ymd') . str_pad(mt_rand(1, 99999), 4, '0', STR_PAD_LEFT);}echo get_order_sn(), PHP_EOL;echo get_order_sn(), PHP_EOL;
201601063753201601063753
$Idwork = new Idwork(1);echo $Idwork->nextId(), PHP_EOL;echo $Idwork->nextId(), PHP_EOL;$test = new Idwork(1);echo $test->nextId(), PHP_EOL;echo $test->nextId(), PHP_EOL;
但并发的时候呢?
令你的第一段代码为 Idwork.php,则
$mch = curl_multi_init();for($i=0; $i<4; $i++) { $ch = curl_init('http://localhost/Idwork.php'); curl_multi_add_handle($mch, $ch);}$running = NULL;do { usleep ( 10000 ); curl_multi_exec ( $mch, $running );} while ( $running > 0 );
8015005880150059801500588015005980150058801500598015005880150059801500588015005980150058801500598015005880150059
get_order_sn方法里面
date('YmdHis'),取到秒,重复的概率就比较小了
我给你一种方法,但是位数的问题就看你自己怎么调整了
有一种不限制数字长度的方法可以使用(可以看出年月日十分秒+随机数)(26位数字)
$order_sn = date('YmdHis').substr(time(),-5).substr(microtime(),2,5).rand(10,99);
优点:
1、不用操作数据库,性能较高。
2、较为直观,不难看出订单产生的大致时间
3、订单号重复的概率极小,只有程序在百万分之一秒内同时处理一个以上的生成订单号请求,而且同时生成的10-99的随机数也一样才会出现重复的订单号。
2016010612111453474171874820160106121114534741718783201601061211145347417187832016010612111453474171871920160106121114534741718730201601061211145347417187772016010612111453474171871320160106121114534741718782
依然不能通过并发测试
只有 com_create_guid 生成全局唯一标识符(GUID)
可确切的保证在同一服务器中不会重复
多台服务器间是否会重复,没有测试不能确认
而 GUID 是号称全球唯一的
只有 com_create_guid 生成全局唯一标识符(GUID)
可确切的保证在同一服务器中不会重复
多台服务器间是否会重复,没有测试不能确认
而 GUID 是号称全球唯一的
只有 com_create_guid 生成全局唯一标识符(GUID)
可确切的保证在同一服务器中不会重复
多台服务器间是否会重复,没有测试不能确认
而 GUID 是号称全球唯一的
时间+用户id+商品序号+随机数
echo uniqid(), PHP_EOL;echo uniqid(), PHP_EOL;
568c83e69c671568c83e69c671
function get_order_sn(){ mt_srand((double) microtime() * 1000000); return date('Ymd') . str_pad(mt_rand(1, 99999), 4, '0', STR_PAD_LEFT);}echo get_order_sn(), PHP_EOL;echo get_order_sn(), PHP_EOL;
201601063753201601063753
$Idwork = new Idwork(1);echo $Idwork->nextId(), PHP_EOL;echo $Idwork->nextId(), PHP_EOL;$test = new Idwork(1);echo $test->nextId(), PHP_EOL;echo $test->nextId(), PHP_EOL;
但并发的时候呢?
echo uniqid(), PHP_EOL;echo uniqid(), PHP_EOL;function get_order_sn(){ mt_srand((double) microtime() * 1000000); return date('Ymd') . str_pad(mt_rand(1, 99999), 4, '0', STR_PAD_LEFT);}echo get_order_sn(), PHP_EOL;echo get_order_sn(), PHP_EOL;
这两种生成的方式 本身无法测试通过 是通过什么方式测试的 是指循环输出吗?
只有 com_create_guid 生成全局唯一标识符(GUID)
可确切的保证在同一服务器中不会重复
多台服务器间是否会重复,没有测试不能确认
而 GUID 是号称全球唯一的
时间+用户id+商品序号+随机数
这个方法是不错,但是不能保证生成的位数 这个订单号会随着用户的id的增长 越来越大
只有 com_create_guid 生成全局唯一标识符(GUID)
可确切的保证在同一服务器中不会重复
多台服务器间是否会重复,没有测试不能确认
而 GUID 是号称全球唯一的
时间+用户id+商品序号+随机数
这个方法是不错,但是不能保证生成的位数 这个订单号会随着用户的id的增长 越来越大
本身你定长的数字 也是有上限的。。。
在一次运行中产生 2 个 id,至少应保证这两个 id 应是不同的
如果连这个都不能保证,那如何能保证两个请求得到的 id 是不同的呢?
你的 Idwork 类可以保证在一次程序运行中不出现重复,但在多次运行中仍会出现重复
com_create_guid 确实很长,但正因为如此,才能保证唯一
从常理可知,要想得到一个不曾出现过的 id,那就要检查他不在已出现过的 id 序列之中
而这个 已出现过的 id 序列 要保存在一个公共的地方,还要防止共享冲突
所以最佳的选择是利用数据库的自增字段
感谢大家的热心回答 又学到了很多知识

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
