PHPackages                             zhujinkui/helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. zhujinkui/helper

ActiveLibrary

zhujinkui/helper
================

This is a tool class library

V1.0(7y ago)0108Apache-2.0PHPPHP &gt;=5.4.0

Since Oct 14Pushed 7y agoCompare

[ Source](https://github.com/zhujinkui/helper)[ Packagist](https://packagist.org/packages/zhujinkui/helper)[ RSS](/packages/zhujinkui-helper/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)DependenciesVersions (2)Used By (0)

idworker
========

[](#idworker)

全局唯一ID生成器，实现了snow flake 算法

Snowflake
=========

[](#snowflake)

SnowFlake的结构如下(每部分用-分开):
*0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000*

- 1位标识，由于long基本类型带符号的，最高位是符号位，正数是0，负数是1，所以id一般是正数，最高位是0
- 41位时间截(毫秒级)，注意，41位时间截不是存储当前时间的时间截，而是存储时间截的差值（当前时间截 - 开始时间截) 得到的值），这里的的开始时间截，一般是我们的id生成器开始使用的时间，由我们程序来指定的（如下下面程序SnowflakeIdWorker类的startTime属性）。 41位的时间截，可以使用69年，年T = (1L &lt;&lt; 41) / (1000L \* 60 \* 60 \* 24 \* 365) = 69
- 10位的数据机器位，可以部署在1024个节点，包括5位datacenterId和5位workerId
- 12位序列，毫秒内的计数，12位的计数顺序号支持每个节点每毫秒(同一机器，同一时间截)产生4096个ID序号
    加起来刚好64位，为一个Long型。
    SnowFlake的优点是，整体上按照时间自增排序，并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分)，并且效率较高，经测试，SnowFlake每秒能够产生26万ID左右。

使用方式
====

[](#使用方式)

```
// 引入文件
$idWorker = new \think\SnowflakeIdWorker(0, 0);
//获取ID
$id       = $idWorker->nextId();
echo $id . PHP_EOL;

```

多机部署
====

[](#多机部署)

```
// 引入文件
$idWorker = new \think\SnowflakeIdWorker(0, 0);

//多机部署的情况下 需要指定工作机器的ID 和 数据中心的ID
//每个节点要保持  $workerId 和  $datacenterId 与其他节点不相同
//另 用一个二维的($workerId,$datacenterId)来唯一标示一个机器
$workerId     = 0;
$datacenterId = 0

//实例化对象
$idWorker = new \think\SnowflakeIdWorker($workerId, $datacenterId);
//获取ID
$id = $idWorker->nextId();
echo $id . PHP_EOL;

```

模拟生成100条
========

[](#模拟生成100条)

```
$idWorker = new \think\SnowflakeIdWorker(0, 0);

for ($i = 0; $i < 100; $i++) {
    $id = $idWorker->nextId();
    echo $id . PHP_EOL;
    echo '';
}

```

ID生成器部署完成以后，需要RPC的方式对外提供服务。

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2768d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/36879659?v=4)[朱金奎](/maintainers/zhujinkui)[@zhujinkui](https://github.com/zhujinkui)

---

Top Contributors

[![zhujinkui](https://avatars.githubusercontent.com/u/36879659?v=4)](https://github.com/zhujinkui "zhujinkui (1 commits)")

### Embed Badge

![Health badge](/badges/zhujinkui-helper/health.svg)

```
[![Health](https://phpackages.com/badges/zhujinkui-helper/health.svg)](https://phpackages.com/packages/zhujinkui-helper)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
