trrtly/php-snowflake
Composer 安装命令:
composer require trrtly/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 algorithm PHP implementation 中文文档.
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
- The first bit is unused sign bit.
- The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
- The 5 bits of the third and fourth parts represent data center and worker, and max value is 2^5 -1 = 31.
- The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond.
- In a distributed environment, five-bit datacenter and worker mean that can deploy 31 datacenters, and each datacenter can deploy up to 31 nodes.
- The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it.
You must know, The ID generated by the snowflake algorithm is not guaranteed to be unique. For example, when two different requests enter the same node of the same data center at the same time, and the sequence generated by the node is the same, the generated ID will be duplicated.
So if you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the same millisecond of the same node is unique. Based on this, we created this package and integrated multiple sequence-number providers into it.
- RandomSequenceResolver (Random)
- RedisSequenceResolver (based on redis psetex and incrby)
- LaravelSequenceResolver (based on redis psetex and incrby)
- SwooleSequenceResolver (based on swoole_lock)
Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
Requirement
- PHP >= 7.0
- Composer
Installation
$ composer require godruoyi/php-snowflake -vvv
Useage
- 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 start time.
$snowflake = new \Godruoyi\Snowflake\Snowflake; $snowflake->setStartTimeStamp(strtotime('2019-09-09')*1000); $snowflake->id();
Advanced
- Used in Laravel.
Because the SDK is relatively simple, we don't provide an extension for Laravel. You can quickly integrate it into Laravel in the following way.
// 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 () { return (new Snowflake()) ->setStartTimeStamp(strtotime('2019-10-10')*1000) ->setSequenceResolver(new LaravelSequenceResolver($this->app->get('cache')->store())); }); } }
- Custom
You can customize the sequence-number resolver by implementing the Godruoyi\Snowflake\SequenceResolver interface.
class YourSequence implements SequenceResolver { /** * {@inheritdoc} */ public function sequence(int $currentTime) { // Just test. return mt_rand(0, 1); } } // usage $snowflake->setSequenceResolver(new YourSequence); $snowflake->id();
And you can use closure:
$snowflake = new \Godruoyi\Snowflake\Snowflake; $snowflake->setSequenceResolver(function ($currentTime) { static $lastTime; static $sequence; if ($lastTime == $currentTime) { ++$sequence; } else { $sequence = 0; } $lastTime = $currentTime; return $sequence; })->id();
License
MIT
trrtly/php-snowflake 适用场景与选型建议
trrtly/php-snowflake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.96k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Unique ID」 「snowflake algorithm」 「php snowflake」 「laravel snowflake」 「order id」 「unique order id」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 trrtly/php-snowflake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 trrtly/php-snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 trrtly/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.
A shared set of open-source enums for frequently used domains, helping reduce duplication and improve consistency across software projects.
Collection of string similarity and distance algorithms in PHP including Levenshtein, Damerau-Levenshtein, Jaro-Winkler, and more
统计信息
- 总下载量: 2.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-09