/php-snowflake

❄ An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

Primary LanguagePHPMIT LicenseMIT

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