bradietilley/laravel-snowflake
Composer 安装命令:
composer require bradietilley/laravel-snowflake
包简介
Laravel implementation for Snowflake IDs
README 文档
README
A Laravel wrapper of PHP Snowflake to provide a simple, opinionated implementation of Snowflake IDs for your eloquent models.
Introduction
The laravel-snowflake package provides a seamless way to generate unique, time-ordered, distributed IDs within your Laravel application. Unlike traditional auto-incrementing IDs, Snowflake IDs offer several advantages: they eliminate ID collisions across distributed systems, enhance horizontal scalability, and prevent predictable sequential exposure of database records. Compared to UUIDs and ULIDs, Snowflake IDs are shorter, visually appealing, and easier to write out, making them ideal for public identifiers. This package integrates smoothly with Laravel, offering microsecond precision while ensuring high-performance, conflict-free ID generation across multiple clusters and workers. 🚀
Installation
composer require bradietilley/laravel-snowflake
Documentation
Preparing your schema:
You'll want to make sure that your model's primary key does not autoincrement. Autoincrement is automatically added when you use $table->id(); so go ahead and switch this out:
-$table->id(); +$table->bigInteger('id')->unsigned()->primary();
Integrating with you models:
Next you'll want to add the HasSnowflake trait to your models. This trait this will handle all aspects of a snowflake ID including:
- Automatically setting the
idto a Snowflake ID - Configuring the cast for
idtostring - Disabling
incrementson the model - Configuring the
keyTypetostring
This can be done as simple as:
use BradieTilley\Snowflakes\Eloquent\HasSnowflake; class SomeModel extends Model { use HasSnowflake; }
You're all set.
$model = SomeModel::create(); $model->id; // 9348975348573485734
Concurrency
The package includes a LaravelSequenceResolver for PHP Snowflake, which utilizes a cache repository to manage the generation of multiple concurrent IDs within the same microsecond.
You can configure various cache-related options, such as:
- The cache store (
snowflakes.sequencing.store) - Cache prefix (
snowflakes.sequencing.prefix) - Cache lock expiry (
snowflakes.sequencing.expiry) - Cache lock wait time (
snowflakes.sequencing.wait)
These settings allow for proper handling of concurrent ID generation in distributed environments.
Testing
In unit testing scenarios, you may want to generate sequential, predictable IDs, similar to traditional auto-incrementing IDs.
By enabling the snowflakes.testing configuration setting, the standard SnowflakeIdentifierResolver is automatically swapped with a SequentialIdentifierResolver. This generates IDs that are realistic in length and follow a standard auto-incrementing pattern.
When in testing mode, Snowflake IDs can be grouped using the $group argument in the BradieTilley\Snowflakes\SnowflakeGenerator::make()->id($group) or BradieTilley\Snowflake\Snowflake::id($group) methods. The $group is automatically set to the respective model class name.
For example, both Product::create() and User::create() generate an ID of 9000000000000000001, then 9000000000000000002, then 9000000000000000003 and so-on.
Author
bradietilley/laravel-snowflake 适用场景与选型建议
bradietilley/laravel-snowflake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bradietilley/laravel-snowflake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bradietilley/laravel-snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-02