Stars: 683
Forks: 115
Pull Requests: 31
Issues: 32
Watchers: 7
Last Updated: 2023-08-28 12:39:08
❄ An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
License: MIT License
Languages: PHP
https://godruoyi.com/posts/php-id-generator-based-on-snowflake-algorithm
Snowflake & Sonyflake algorithm PHP implementation 中文文档.
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
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.
If you want to 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.
fopen/flock
, Concurrency Safety)Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
octicon-alert mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
The RandomSequenceResolver does not guarantee that the generated IDs are unique, If you want to generate a unique ID, please use another resolver instead.
$ composer require godruoyi/php-snowflake -vvv
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->id();
// 1537200202186752
$snowflake = new \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId);
$snowflake->id();
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setStartTimeStamp(strtotime('2019-09-09')*1000); // millisecond
$snowflake->id();
$sonyflake = new \Godruoyi\Snowflake\Sonyflake;
$sonyflake->id();
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 ($app) {
return (new Snowflake())
->setStartTimeStamp(strtotime('2019-10-10')*1000)
->setSequenceResolver(new LaravelSequenceResolver($app->get('cache.store')));
});
}
}
You can customize the sequence-number resolver by implementing 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 can use 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();
MIT