生成唯一ID算法——薄雾算法
实现算法
/*
* 薄雾算法
*
* 1 2 48 56 64
* +------+-----------------------------------------------------+----------+----------+
* retain | increas | salt | sequence |
* +------+-----------------------------------------------------+----------+----------+
* 0 | 0000000000 0000000000 0000000000 0000000000 0000000 | 00000000 | 00000000 |
* +------+-----------------------------------------------------+------------+--------+
*
* 0. 最高位,占 1 位,保持为 0,使得值永远为正数;
* 1. 自增数,占 47 位,自增数在高位能保证结果值呈递增态势,遂低位可以为所欲为;
* 2. 随机因子一,占 8 位,上限数值 255,使结果值不可预测;
* 3. 随机因子二,占 8 位,上限数值 255,使结果值不可预测;
*
* 编号上限为百万亿级,上限值计算为 140737488355327 即 int64(1 << 47 - 1),假设每天取值 10 亿,能使用 385+ 年
*/
class Mist
{
public const saltBit = 8; // 随机因子二进制位数
public const saltShift = 8; // 随机因子移位数
public const increasShift = self::saltBit + self::saltShift; // 自增数移位数
public $increas = 0; // 自增数
public $saltA = 0; // 随机因子一
public $saltB = 0; // 随机因子二
public function __construct($increas = 1)
{
$this->increas = $increas;
}
public function generate(): int
{
$this->increas = ++$this->increas;
// 获取随机因子数值
try {
$this->saltA = random_int($this->saltA, 255);
} catch (\Exception $e) {
$this->saltA = $this->saltA++;
}
try {
$this->saltB = random_int($this->saltB, 255);
} catch (\Exception $e) {
$this->saltB = $this->saltB++;
}
return (int) ($this->increas << self::increasShift) | ($this->saltA << self::saltShift) | $this->saltB;
}
}
调用如下:
$uuid = (new Mist(1))->generate();
#> > 153290