PHP高并发生成不重复唯一标识

本文适用于所有需要保证标识唯一性的场景

一、模拟场景

假设唯一标识为用户ID,当有3个用户同时注册,生成用户ID的时间完全相同,如何保证ID的唯一性

二、已有解决方案?

网友们给出的方案很多,其中比较流行的方案如下所示:

md5(uniqid(md5(microtime(true)),true));
microtime( TRUE ) - 返回unix纪元以来的当前时间,精确到最接近的微秒(1568049494.73)

md5 ( string $str [, bool $raw_output = FALSE ] ) : string - 返回长度为32的原始十六进制格式数据

md5 ( string $str [, bool $raw_output = TRUE ] ) : string - 返回长度为16的原始二进制格式数据

uniqid ([ string $prefix = "" [, bool $more_entropy = TRUE ]] ) : string - 如果设置为 TRUE,uniqid() 会在返回的字符串结尾增加额外的熵(使用combined linear congruential generator), 使得唯一ID更具唯一性

从上面几个函数的功能来看,保证唯一性主要靠uniqid,接着看看该函数的源码:

PHP_FUNCTION(uniqid)
{
    ...
    gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
    sec = (int) tv.tv_sec;
    usec = (int) (tv.tv_usec % 0x100000);

    ...
    if (more_entropy) {
        uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg() * 10);
    } else {
        uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
    }

    RETURN_STR(uniqid);
}

uniqid 是由四个部分组成:

prefix - 调用者传递的字符串参数
sec - 当前时钟的秒
usec - 当前时钟的微秒
php_combined_lcg - 使用线性同余生成的一个墒值,为0 ~ 1 之间的随机数(如 0.12345678),由参数more_entropy决定是否生成

在高并发情况下,php1、php2、php3 在微秒级同时操作,sec、usec 均相同。当前缀 prefix 也相同时,保证唯一性只能靠php_combined_lcg,源码:

https://github.com/php/php-src/blob/master/ext/standard/lcg.c

/*
 * combinedLCG() returns a pseudo random number in the range of (0, 1).
 * The function combines two CGs with periods of
 * 2^31 - 85 and 2^31 - 249. The period of this function
 * is equal to the product of both primes.
 */

#define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m

static void lcg_seed(void);

PHPAPI double php_combined_lcg(void) /* {{{ */
{
	int32_t q;
	int32_t z;

	if (!LCG(seeded)) {
		lcg_seed();
	}

	MODMULT(53668, 40014, 12211, 2147483563L, LCG(s1));
	MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2));

	z = LCG(s1) - LCG(s2);
	if (z < 1) {
		z += 2147483562;
	}

	return z * 4.656613e-10;
}
/* }}} */

三、优化方案

由上面的分析可以看出,如果我们单纯使用 uniqid() 这个方法,不带任何参数的情况下只能保证单个进程/线程,在微秒级以上(毫秒级)是唯一的。如果使用uniqid(string $prefix, true), 增加墒值使用一个随机的方式保证唯一性。但是由于线性同余是比较简单的生成随机数的算法,随机性不足,所以,更优的方式是让 prefix 参数也不相同:

uniqid(mt_rand(), true)

其中 mt_rand() 生成随机数使用 Mersenne Twister Random Number Generator (梅森旋转算法),而不再是线性同余,范围为0 ~ 2147483647。换句话说,上面这个 id 由两种随机算法 + 时间戳生成。基本上,这个算法在很大程度上能保证唯一性了。如果需要固定位数的hash值,例如32位:

md5(uniqid(mt_rand(), true))

如果需要传参,例如用户使用手机号码注册,可以表示为:

<?php

$phone = '18600000000';
// 长度40
$uid = sha1(md5(uniqid(mt_rand(), true)).$phone);

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值