ioext / dedid
ioext公司提供的分布式数据库唯一ID生成器。
v1.0.32
2023-02-25 08:08 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ~6.0
README
用于分布式数据库主键的唯一ID生成器。此算法实现参考了Twitter的Snowflake算法,但在最后的12位中,不仅可以使用随机数,还可以通过指定的字符串获取哈希值。
ALGORITHM
位结构
64位长整型。
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxx xxxxx xxxx xxxxxxxx
详细信息
位标记
中心
0 00000000 00000000 00000000 00000000 00000000 0 11111 00000 0000 00000000
00000000 00000000 00000000 00000000 00000000 00111110 00000000 00000000
00 00 00 00 00 3E 00 00
节点
0 00000000 00000000 00000000 00000000 00000000 0 00000 11111 0000 00000000
00000000 00000000 00000000 00000000 00000000 00000001 11110000 00000000
00 00 00 00 00 01 F0 00
逃逸时间
0 11111111 11111111 11111111 11111111 11111111 1 00000 00000 0000 00000000
01111111 11111111 11111111 11111111 11111111 11000000 00000000 00000000
7F FF FF FF FF C0 00 00
随机或哈希值
0 00000000 00000000 00000000 00000000 00000000 0 00000 00000 1111 11111111
00000000 00000000 00000000 00000000 00000000 00000000 00001111 11111111
00 00 00 00 00 00 0F FF
如何使用
正常创建新ID
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 1;
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, null, $arrD );
echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );
输出
new id = 114654484990270790
Array
(
[center] => 0
[node] => 1
[time] => 27335759399
[rand] => 3398
)
通过指定的字符串创建带有crc32哈希值的新ID
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 15;
$sSrc = "ioext";
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, $sSrc, $arrD );
echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );
输出
new id = 114654631304370386
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
解析ID以获取详细信息
$cDId = CDId::getInstance();
$arrId = $cDId->parseId( 114654631304370386 );
print_r( $arrId );
输出
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
安装
# composer require ioext/dedid