Download the PHP package qs9000/think-uuidv7 without Composer
On this page you can find all versions of the php package qs9000/think-uuidv7. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download qs9000/think-uuidv7
More information about qs9000/think-uuidv7
Files in qs9000/think-uuidv7
Download qs9000/think-uuidv7
More information about qs9000/think-uuidv7
Files in qs9000/think-uuidv7
Vendor qs9000
Package think-uuidv7
Short Description ThinkPHP8 Redis-based UUIDv7 Generator for Distributed Systems
License Apache-2.0
Package think-uuidv7
Short Description ThinkPHP8 Redis-based UUIDv7 Generator for Distributed Systems
License Apache-2.0
Please rate this library. Is it a good library?
Informations about the package think-uuidv7
think-uuidv7
基于 ThinkPHP 8 的 UUIDv7 生成器,专为分布式系统设计。
特性
- RFC 9562 兼容 - 严格遵循 UUIDv7 标准
- 自动安装 - Composer 安装后自动配置,无需手动复制文件
- 二进制存储 - 16 字节二进制格式,节省 55% 存储空间
- 分片支持 - 内置 shard_id (0-255),支持 256 个节点
- 分布式序列号 - Redis Lua 脚本保证原子性
- 批量生成 - 支持批量生成单调递增 UUID
- ORM 集成 - ThinkPHP 模型类型转换器
- 时间可排序 - 同一节点内完全可排序
安装
安装后自动完成:
- 配置文件复制到
config/uuidv7.php - 服务自动注册
快速开始
1. 注册服务(可选)
如果自动注册失败,在 application/provider.php 中手动注册:
2. 配置
配置文件已自动生成在 config/uuidv7.php:
3. 生成 UUID
4. 解析 UUID
5. 二进制格式
数据库存储使用 BINARY(16) 而非 CHAR(36):
ThinkPHP 模型集成
类型转换器
在模型中定义 uuidv7 类型字段:
工作原理:
- 写入时:UUID 字符串 → 二进制
- 读取时:二进制 → UUID 字符串
数据库迁移
UUID 结构
| 字段 | 位长 | 说明 |
|---|---|---|
| timestamp | 48bit | 毫秒级时间戳(自 Unix 纪元) |
| version | 4bit | 版本号,固定为 7 |
| rand_a | 12bit | shard_id(8bit高) + seq_high(4bit低) |
| sequence | 40bit | 毫秒内序列号(Redis支持40bit,本地12bit) |
| variant | 2bit | 变种位,固定为 10 |
| rand_b | 14bit | seq_low(8bit) + random(6bit) |
| rand_c | 48bit | 随机数 |
说明:
shard_id存储在 rand_a 高 8 位,支持 256 个节点sequence编码在 rand_a + rand_b 中,本地同毫秒可生成 4096 个 ID- 使用 Redis 时,全局序列号支持 2^40 级别
时间戳提取
分布式配置
多节点部署
每个服务器设置唯一的 shard_id:
Redis 配置
性能特性
| 场景 | 单次生成 | 批量生成 (100) |
|---|---|---|
| 本地模式 | ~0.02ms | ~0.5ms |
| Redis 模式 | ~0.1ms | ~1ms |
- 随机字节缓存 - 批量生成时复用随机字节,减少系统调用
- 本地序列号 - 同一毫秒内无需 Redis 往返
- Redis Lua 脚本 - 原子操作,避免竞态条件
容量分析
| 存储方式 | 序列号容量 | 时间范围 |
|---|---|---|
| 本地模式 | 4,096/毫秒 | 无限(按 shard_id 分片) |
| Redis 模式 | 1万亿/毫秒 | 约 35,000 年 |
CLI 命令
测试
依赖
- PHP >= 8.0
- topthink/framework ^8.0
- phpunit/phpunit ^10.0 (dev)
License
Apache-2.0
All versions of think-uuidv7 with dependencies
PHP Build Version
Package Version
The package qs9000/think-uuidv7 contains the following files
Loading the files please wait ...