ifullgaz/laravel-global-unique-id

为模型分配全局唯一ID

1.0.1 2022-07-17 01:45 UTC

This package is auto-updated.

Last update: 2024-09-17 06:43:39 UTC


README

Unit Tests License Latest Stable Version Latest Unstable Version PHP Version Require Total Downloads

特质用于为所有运行的PHP进程生成64位全局唯一ID。唯一ID是一个64位整数。ID保证按时间顺序排列。整数的具体内容如下,例如

63 62 ------------------------------------------ 21 20 ------------ 9 8 ---------- 0
 0           42 bits time in milliseonds                 12 bits          9 bits
            140 years until rollover to 0           unique machine id  local counter

注意:由于PHP不支持无符号整数,第63位必须是0。

在这个例子中,每个PHP进程在发生任何碰撞之前,可以每毫秒创建最多512个新ID。在这个系统中,每毫秒可以创建总共2,097,152个新ID。

注意:目前JS无法处理大于2^53-1的整数。一个很好的折衷方案是使用42-6-5位作为值,允许每毫秒有64 * 32 = 2048个新ID。如果这不合适(不够每秒ID),可以在反序列化之前使用以下代码片段来解析原始响应

function (data) {
    data = data.replace(/:\s*(-?\d+),/g, (match, $1) => {
        if ($1 <= Number.MAX_SAFE_INTEGER) {
            return match;
        }
        return ':"'+$1+'",';
    });
    return data;
}

要求

此包需要Laravel 9及以上版本、apcu和redis php扩展以及redis服务器。

安装

使用composer要求此包。

composer require ifullgaz/laravel-global-unique-id

Laravel使用包自动发现,因此不需要手动添加ServiceProvider。

Laravel无自动发现

如果您不使用自动发现,请将ServiceProvider添加到config/app.php中的providers数组中

Ifullgaz\GlobalUniqueId\ServiceProvider::class,

使用publish命令将包配置复制到本地配置

php artisan vendor:publish --tag=globaluniqueid-config

选项

以下选项可以在globaluniqueid.php文件中配置

    /*
|--------------------------------------------------------------------------
| Start date
|--------------------------------------------------------------------------
|
| Starting reference date. Should be before today.
|
*/
'start_date' => env('GLOBAL_UNIQUE_ID_START_DATE', '01/01/2022'),

/*
|--------------------------------------------------------------------------
| Timestamp size
|--------------------------------------------------------------------------
|
| Number of bits to use for the timestamp.
| The more bits used, the larger the date range
| eg: 42 bits -> ~140 years, 41 bits -> ~70 years...
|
*/
'timestamp_size' => env('GLOBAL_UNIQUE_ID_TIMESTAMP_SIZE', 42),

/*
|--------------------------------------------------------------------------
| Machine id size
|--------------------------------------------------------------------------
|
| Number of bits to use for the machine id.
| The more bits used, the more PHP processes can run concurrently.
| eg: 11 bits -> 2^11 (2048) concurrent processes
|
*/
'machine_id_size' => env('GLOBAL_UNIQUE_ID_MACHINE_ID_SIZE', 11),

/*
|--------------------------------------------------------------------------
| Counter size
|--------------------------------------------------------------------------
|
| Number of bits to use for the local counter.
| The more bits used, the higher the local counter can go.
| eg: 10 bits -> 2^10 (1024) values
|
*/
'counter_size' => env('GLOBAL_UNIQUE_ID_COUNTER_SIZE', 10),

用法

将特质添加到一个具有数值主键的类中,如下所示

use Ifullgaz\GlobalUniqueId\Models\Traits\GlobalUniqueId;

class MyClass extends Model
{
    use GlobalUniqueId;
}