Skip to content

Commit cbf4476

Browse files
authored
chore: remove files and update readme (#74)
* chore: remove riles and update readmd * chore: update readme
1 parent ee1f3af commit cbf4476

File tree

4 files changed

+30
-51
lines changed

4 files changed

+30
-51
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ phpunit.php export-ignore
1010
phpunit.xml.dist export-ignore
1111
phpunit.xml export-ignore
1212
.php_cs export-ignore
13+
pint.json export-ignore
14+
phpstan.neon.dist export-ignore

CHANGELOG.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

README-zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Snowflake 是 Twitter 内部的一个 ID 生算法,可以通过一些简单的
4646

4747
* RandomSequenceResolver(随机生成)
4848
* RedisSequenceResolver (基于 redis psetex 和 incrby 生成)
49+
* PredisSequenceResolver (基于 redis psetex 和 incrby 生成)
4950
* LaravelSequenceResolver(基于 redis psetex 和 incrby 生成)
5051
* SwooleSequenceResolver(基于 swoole_lock 锁)
5152
* FileLockResolver(基于 PHP 文件锁)

README.md

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,22 @@ Snowflake & Sonyflake algorithm PHP implementation [中文文档](https://github
2828

2929
![file](https://images.godruoyi.com/logos/201908/13/_1565672621_LPW65Pi8cG.png)
3030

31-
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
31+
Snowflake is a network service that generates unique ID numbers at high scale with simple guarantees.
3232

33-
* The first bit is unused sign bit.
34-
* The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
35-
* The 5 bits of the third and fourth parts represent data center and worker, and max value is 2^5 -1 = 31.
36-
* The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond.
37-
* In a distributed environment, five-bit datacenter and worker mean that can deploy 31 datacenters, and each datacenter can deploy up to 31 nodes.
38-
* The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it.
33+
1. The first bit is an unused sign bit.
34+
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.
35+
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).
36+
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).
3937

40-
> You must know, The ID generated by the snowflake algorithm is not guaranteed to be unique.
41-
> 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.
38+
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.
39+
Based on this requirement, we have created this package which integrates multiple sequence number providers.
4240

43-
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.
44-
Based on this, we created this package and integrated multiple sequence-number providers into it.
45-
46-
* RandomSequenceResolver (Random)
47-
* FileLockResolver(PHP file lock `fopen/flock`, **Concurrency Safety**
48-
* RedisSequenceResolver (based on redis psetex and incrby, **Concurrency Safety**)
49-
* LaravelSequenceResolver (based on Laravel Cache [add](https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Cache/Repository.php#L39) lock)
50-
* SwooleSequenceResolver (based on swoole_lock)
51-
* PredisSequenceResolver (based on redis psetex and incrby, **Concurrency Safety**)
52-
53-
Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
54-
55-
56-
> [!NOTE]
57-
> If you want to use RedisSequenceResolver, please install the [redis](https://pecl.php.net/package/redis) extension:
58-
> pecl install redis
59-
>
60-
> If you want to use SwooleSequenceResolver, please install the swoole extension:
61-
> pecl install swoole
62-
>
63-
> If you want to use PredisSequenceResolver, please install the [predis/predis](https://github.com/predis/predis) package:
64-
> composer install predis/predis
65-
66-
> **Warning**
67-
> The RandomSequenceResolver does not guarantee that the generated IDs are unique, If you want to generate a unique ID, please use another resolver instead.
41+
* RandomSequenceResolver (Random Sequence Number, UnSafe)
42+
* FileLockResolver (Uses PHP file lock `fopen/flock`, **Concurrency Safety**)
43+
* RedisSequenceResolver (Redis psetex and incrby, **Concurrency safety**)
44+
* PredisSequenceResolver (redis psetex and incrby, **Concurrency Safety**)
45+
* LaravelSequenceResolver (Laravel Cache [add](https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Cache/Repository.php#L39) lock mechanism)
46+
* SwooleSequenceResolver (swoole_lock for **Concurrency Safety**)
6847

6948
## Requirement
7049

@@ -75,6 +54,15 @@ Each provider only needs to ensure that the serial number generated in the same
7554

7655
```shell
7756
$ composer require godruoyi/php-snowflake -vvv
57+
58+
# Install `predis/predis` package if you are using PredisSequenceResolver
59+
$ composer require "predis/predis"
60+
61+
# Install `Redis` extensions if you are using RedisSequenceResolver
62+
$ pecl install redis
63+
64+
# Install `Swoole` extensions if you are using SwooleSequenceResolver
65+
$ pecl install swoole
7866
```
7967

8068
## Usage
@@ -105,6 +93,8 @@ $snowflake->setStartTimeStamp(strtotime('2019-09-09')*1000); // millisecond
10593
$snowflake->id();
10694
```
10795

96+
> 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.
97+
10898
4. Use Sonyflake
10999

110100
```php
@@ -116,7 +106,7 @@ $sonyflake->id();
116106

117107
1. Used in Laravel.
118108

119-
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.
109+
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.
120110

121111
```php
122112
// App\Providers\AppServiceProvider
@@ -144,7 +134,7 @@ class AppServiceProvider extends ServiceProvider
144134

145135
2. Custom
146136

147-
You can customize the sequence-number resolver by implementing the Godruoyi\Snowflake\SequenceResolver interface.
137+
To customize the sequence number resolver, you need to implement the Godruoyi\Snowflake\SequenceResolver interface.
148138

149139
```php
150140
class YourSequence implements SequenceResolver
@@ -165,7 +155,7 @@ $snowflake->setSequenceResolver(new YourSequence);
165155
$snowflake->id();
166156
```
167157

168-
And you can use closure:
158+
And you also can use the Closure:
169159

170160
```php
171161
$snowflake = new \Godruoyi\Snowflake\Snowflake;

0 commit comments

Comments
 (0)