godruoyi/php-snowflake
Composer 安装命令:
composer require godruoyi/php-snowflake
包简介
An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
关键字:
README 文档
README
An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
Description
Snowflake & Sonyflake algorithm PHP implementation 中文文档.
Snowflake is a network service that generates unique ID numbers at a high scale with simple guarantees.
- The first bit is an unused sign bit.
- The second part consists of a 41-bit timestamp (in milliseconds) representing the offset of the current time relative to a certain reference time.
- The third and fourth parts are represented by 5 bits each, indicating the data centerID and workerID. The maximum value for both is 31 (2^5 -1).
- The last part consists of 12 bits, which represents the length of the serial number generated per millisecond per working node. A maximum of 4095 IDs can be generated in the same millisecond (2^12 -1).
If you want to generate unique IDs using the snowflake algorithm, you must ensure that sequence numbers generated within the same millisecond on the same node are unique. Based on this requirement, we have created this package which integrates multiple sequence number providers.
- RandomSequenceResolver (Random Sequence Number, UnSafe)
- FileLockResolver (Uses PHP file lock
fopen/flock, Concurrency Safety) - RedisSequenceResolver (Redis psetex and incrby, Concurrency safety)
- PredisSequenceResolver (redis psetex and incrby, Concurrency Safety)
- LaravelSequenceResolver (Laravel Cache add lock mechanism)
- SwooleSequenceResolver (swoole_lock for Concurrency Safety)
Requirement
- PHP >= 8.1
- Composer
Installation
$ composer require godruoyi/php-snowflake -vvv # Install `predis/predis` package if you are using PredisSequenceResolver $ composer require "predis/predis" # Install `Redis` extensions if you are using RedisSequenceResolver $ pecl install redis # Install `Swoole` extensions if you are using SwooleSequenceResolver $ pecl install swoole
Usage
- simple to use.
$snowflake = new \Godruoyi\Snowflake\Snowflake; $snowflake->id(); // 1537200202186752
- Specify the data center ID and machine ID.
$snowflake = new \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId); $snowflake->id();
- Specify the start time.
$snowflake = new \Godruoyi\Snowflake\Snowflake; $snowflake->setStartTimeStamp(strtotime('2019-09-09')*1000); // millisecond $snowflake->id();
The maximum value of a 41-bit timestamp (in milliseconds) can represent up to 69 years, so the Snowflake algorithm can run safely for 69 years. In order to make the most of it, we recommend setting a start time.
- Using different sequence number resolvers (optional).
$snowflake = new \Godruoyi\Snowflake\Snowflake; $snowflake->setSequenceResolver(new \Godruoyi\Snowflake\RandomSequenceResolver); $snowflake->id();
- Use Sonyflake
$sonyflake = new \Godruoyi\Snowflake\Sonyflake; $sonyflake->id();
Advanced
- Used in Laravel.
Since the SDK is quite straightforward, we do not offer a specific extension for Laravel. However, you can easily integrate it into your Laravel project by following these steps.
// App\Providers\AppServiceProvider use Godruoyi\Snowflake\Snowflake; use Godruoyi\Snowflake\LaravelSequenceResolver; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { $this->app->singleton('snowflake', function ($app) { return (new Snowflake()) ->setStartTimeStamp(strtotime('2019-10-10')*1000) ->setSequenceResolver(new LaravelSequenceResolver($app->get('cache.store'))); }); } }
- Custom
To customize the sequence number resolver, you need to implement the Godruoyi\Snowflake\SequenceResolver interface.
class YourSequence implements SequenceResolver { /** * {@inheritdoc} */ public function sequence(int $currentMillisecond) { // Just test. return mt_rand(0, 1); } } // usage $snowflake->setSequenceResolver(new YourSequence); $snowflake->id();
And you also can use the Closure:
$snowflake = new \Godruoyi\Snowflake\Snowflake; $snowflake->setSequenceResolver(function ($currentMillisecond) { static $lastTime; static $sequence; if ($lastTime == $currentMillisecond) { ++$sequence; } else { $sequence = 0; } $lastTime = $currentMillisecond; return $sequence; })->id();
License
MIT
godruoyi/php-snowflake 适用场景与选型建议
godruoyi/php-snowflake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.56M 次下载、GitHub Stars 达 862, 最近一次更新时间为 2019 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Unique ID」 「snowflake algorithm」 「php snowflake」 「laravel snowflake」 「order id」 「unique order id」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 godruoyi/php-snowflake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 godruoyi/php-snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 godruoyi/php-snowflake 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel queue connection that prevents identical jobs from being queued
Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
A Monolog processor to add X-Request-Id or UNIQUE_ID env to monolog stack if present, provide a default one otherwise. Optionally, add a forwarded request-id list.
An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
A shared set of open-source enums for frequently used domains, helping reduce duplication and improve consistency across software projects.
统计信息
- 总下载量: 2.56M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 880
- 点击次数: 27
- 依赖项目数: 64
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-12