godruoyi / php-snowflake
An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
Fund package maintenance!
images.godruoyi.com/wechat.png
Installs: 1 345 938
Dependents: 53
Suggesters: 1
Security: 0
Stars: 788
Watchers: 8
Forks: 97
Open Issues: 0
Requires
- php-64bit: >=8.1
Requires (Dev)
- ext-redis: *
- ext-swoole: *
- illuminate/contracts: ^10.0 || ^11.0
- laravel/pint: ^1.10
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10
- predis/predis: ^2.0
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 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 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.
- 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