Download the PHP package godruoyi/php-snowflake without Composer
On this page you can find all versions of the php package godruoyi/php-snowflake. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download godruoyi/php-snowflake
More information about godruoyi/php-snowflake
Files in godruoyi/php-snowflake
Package php-snowflake
Short Description An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
License MIT
Homepage https://github.com/godruoyi/php-snowflake
Informations about the package php-snowflake
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
Usage
-
simple to use.
-
Specify the data center ID and machine ID.
- Specify start time.
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
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.
- Custom
To customize the sequence number resolver, you need to implement the Godruoyi\Snowflake\SequenceResolver interface.
And you also can use the Closure:
License
MIT