godruoyi/php-snowflake 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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).

build passed License Packagist Version Total Downloads

Description

Snowflake & Sonyflake algorithm PHP implementation 中文文档.

file

Snowflake is a network service that generates unique ID numbers at a high scale with simple guarantees.

  1. The first bit is an unused sign bit.
  2. The second part consists of a 41-bit timestamp (in milliseconds) representing the offset of the current time relative to a certain reference time.
  3. 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).
  4. 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

  1. PHP >= 8.1
  2. 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

  1. simple to use.
$snowflake = new \Godruoyi\Snowflake\Snowflake;

$snowflake->id();
// 1537200202186752
  1. Specify the data center ID and machine ID.
$snowflake = new \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId);

$snowflake->id();
  1. 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.

  1. Using different sequence number resolvers (optional).
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setSequenceResolver(new \Godruoyi\Snowflake\RandomSequenceResolver);

$snowflake->id();
  1. Use Sonyflake
$sonyflake = new \Godruoyi\Snowflake\Sonyflake;

$sonyflake->id();

Advanced

  1. 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')));
        });
    }
}
  1. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 godruoyi/php-snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.56M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 880
  • 点击次数: 27
  • 依赖项目数: 64
  • 推荐数: 1

GitHub 信息

  • Stars: 862
  • Watchers: 5
  • Forks: 99
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-12