lizhineng/snowflake 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lizhineng/snowflake

Composer 安装命令:

composer require lizhineng/snowflake

包简介

Build your own Snowflake.

README 文档

README

A flexible, customizable distributed ID generator inspired by Twitter's Snowflake, built for PHP 8.2+.

Unlike traditional implementations with hard-coded structures, this package lets you build your own ID format to perfectly suit your application's needs.

Installation

The package can be installed through Composer:

composer require lizhineng/snowflake

Requirements: PHP 8.2 or higher

Quick Start

use Zhineng\Snowflake\Constant;
use Zhineng\Snowflake\Manager;
use Zhineng\Snowflake\Sequence;
use Zhineng\Snowflake\Structure;
use Zhineng\Snowflake\Timestamp;

$structure = new Structure;
$structure->add(Sequence::make('sequence', 12));
$structure->add(Constant::make('instance', 10));
$structure->add(Timestamp::make('timestamp', 41));

$manager = new Manager;
$manager->structureUsing($structure);

$id1 = $manager->nextId(); // e.g., 123456789012345678
$id2 = $manager->nextId(); // e.g., 123456789012345679

Usage

Field Types

Timestamp

Timestamp field tracks milliseconds since epoch, defaulting to 41 bits and the name timestamp:

Timestamp::make();

Optionally set a custom epoch via the third parameter—accepts a DateTime object or an integer (milliseconds since Unix epoch):

$epoch = new \DateTime('2026-01-01 00:00:00');
Timestamp::make('timestamp', 41, $epoch);

Constant

Fixed value field for static identifiers (e.g., datacenter ID, machine ID). Value defaults to 0 if not specified.

// Machine ID with value 5 (10 bits)
Constant::make('machine_id', 10, 5);

// Datacenter ID with value 2 (5 bits)
Constant::make('datacenter_id', 5, 2);

Sequence

Auto-incrementing sequence that resets to 0 whenever any other field changes. An OverflowException is thrown if the value exceeds its maximum.

// 12-bit sequence (range 0-4095)
Sequence::make('sequence', 12);

Building Custom Structures

Customize the ID format to suit your needs—the structure is built from right to left (LSB to MSB):

$structure = new Structure;

// Bit layout: [timestamp:41][instance:10][sequence:12] = 63 bits
$structure->add(Sequence::make('sequence', 12));
$structure->add(Constant::make('instance', 10));
$structure->add(Timestamp::make('timestamp', 41));

echo $structure->size(); // 63

Testing

Run the test suite locally:

composer test

Test across all PHP versions and dependency modes:

composer matrix

License

MIT License. See LICENSE for details.

lizhineng/snowflake 适用场景与选型建议

lizhineng/snowflake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lizhineng/snowflake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lizhineng/snowflake 我们能提供哪些服务?
定制开发 / 二次开发

基于 lizhineng/snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-06