Skip to content

Commit

Permalink
chore: remove riles and update readmd
Browse files Browse the repository at this point in the history
  • Loading branch information
godruoyi committed Apr 8, 2024
1 parent b8dfbc3 commit ae4094e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
pint.json export-ignore
phpstan.neon.dist export-ignore
14 changes: 0 additions & 14 deletions CHANGELOG.md

This file was deleted.

1 change: 1 addition & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Snowflake 是 Twitter 内部的一个 ID 生算法,可以通过一些简单的

* RandomSequenceResolver(随机生成)
* RedisSequenceResolver (基于 redis psetex 和 incrby 生成)
* PredisSequenceResolver (基于 redis psetex 和 incrby 生成)
* LaravelSequenceResolver(基于 redis psetex 和 incrby 生成)
* SwooleSequenceResolver(基于 swoole_lock 锁)
* FileLockResolver(基于 PHP 文件锁)
Expand Down
56 changes: 23 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,24 @@ Snowflake & Sonyflake algorithm PHP implementation [中文文档](https://github

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

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

* The first bit is unused sign bit.
* The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
* The 5 bits of the third and fourth parts represent data center and worker, and max value is 2^5 -1 = 31.
* 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.
* In a distributed environment, five-bit datacenter and worker mean that can deploy 31 datacenters, and each datacenter can deploy up to 31 nodes.
* 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.
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).

> 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.
> With a binary length of 41 bits, it can represent up to 69 years' worth of milliseconds (2^41 -1). Therefore, the snowflake algorithm can be used for up to 69 years. To maximize its use, it's recommended to specify a start time.
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.
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)
* FileLockResolver(PHP file lock `fopen/flock`, **Concurrency Safety**
* RedisSequenceResolver (based on redis psetex and incrby, **Concurrency Safety**)
* LaravelSequenceResolver (based on Laravel Cache [add](https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Cache/Repository.php#L39) lock)
* SwooleSequenceResolver (based on swoole_lock)
* PredisSequenceResolver (based on redis psetex and incrby, **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.


> [!NOTE]
> If you want to use RedisSequenceResolver, please install the [redis](https://pecl.php.net/package/redis) extension:
> pecl install redis
>
> If you want to use SwooleSequenceResolver, please install the swoole extension:
> pecl install swoole
>
> If you want to use PredisSequenceResolver, please install the [predis/predis](https://github.com/predis/predis) package:
> composer install predis/predis
> **Warning**
> The RandomSequenceResolver does not guarantee that the generated IDs are unique, If you want to generate a unique ID, please use another resolver instead.
* 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](https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Cache/Repository.php#L39) lock mechanism)
* SwooleSequenceResolver (swoole_lock for **Concurrency Safety**)

## Requirement

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

```shell
$ 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
Expand Down

0 comments on commit ae4094e

Please sign in to comment.