phossa2 / uuid
一个用于在数据库中作为主键使用的基于顺序和时间生成的UUID的PHP库。
Requires
- php: ~5.4|~7.0
- phossa2/shared: ^2.0.21
Requires (Dev)
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: 2.*
Provides
- psr/log-implementation: 1.0.0
This package is not auto-updated.
Last update: 2024-09-14 19:04:15 UTC
README
phossa2/uuid是一个用于生成顺序UUID的PHP库,可用于数据库中的主键。
它需要PHP 5.4,支持PHP 7.0+和HHVM。它符合PSR-1、PSR-2、PSR-3、PSR-4和提议的PSR-5。
安装
通过composer
工具安装。
composer require "phossa2/uuid"
或者将以下行添加到您的composer.json
{ "require": { "phossa2/uuid": "2.*" } }
功能
-
根据文章以优化的方式存储UUID,非有序UUID会对Mysql数据库插入性能产生重大影响。
-
我们不是遵循RFC 4122来生成UUID,而是采用了一个内置数据类型的新设计。例如,用户ID的类型为
1010
。任何使用此库的用户ID都将以'2101-0'开头 -
内置分片位,可以轻松地对数据库表进行分片。
-
只要时间算法足够好,它将保证至少在一个供应商的家中具有唯一性。
设计
使用32个字符,不带-
2xxx - xxxx - xxxx - xxxx - xxxx - xxxx - xxxx - xxxx
^ ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^^^ ^^^^^^
ver type timestamp shard vendor remain
-
版本:位置0,1个字符
-
此UUID库的版本
-
默认为
2
-
-
数据类型:位置1-4,4个字符
-
16位,65535
-
库预留类型
1***
-
自定义类型从
[2-f]***
开始
-
-
时间戳:位置5-19,15个字符
-
60位
-
可用于至少360年
-
-
分片:位置20-23,4个字符
-
16位,65535
-
用于分片目的,由用户提供
-
-
供应商:位置24-27(4个字符)
- 由用户提供的供应商ID
-
剩余:位置28-31(4个字符)
- 供未来使用
用法
use Phossa2\Uuid\Uuid; // 2100020bc58eb7f18602000100010000 $uuid = Uuid::get(); // encode/shorten it, can be used in URL if (Uuid::isValid($uuid)) { // AWprUw7urpN8bbQ4LciGNa $short = Uuid::encode($uuid); // decode var_dump($uuid === Uuid::decode($short)); // true }
使用您自己的设置或算法扩展Phossa2\Uuid\Uuid
class MyUuid extends Uuid { /* * use this vendor id * * {@inheritDoc} */ protected $vendor = '1234'; /* * use this more reliable sequence * * {@inheritDoc} */ protected function getSequence() { // ... } }
APIs
-
-
Uuid::get(string $dataType, string $shardId): string
两个参数都是可选的。
-
-
-
Uuid::isValid(string $uuid): bool
检查
$uuid
是否有效。 -
Uuid::info(string $uuid): array
获取关于此
$uuid
的详细信息,包括version
、type
、time
、vendor
、remain
。 -
Uuid::encode(string $uuid): string
将
$uuid
编码为短版本(base56) -
Uuid::decode(string $string): string
将短版本解码为完整的32个字符UUID
-
预定义数据类型
-
通用OID
UuidInterface::TYPE_OID
,值1000
。 -
用户ID
UuidInterface::TYPE_USER
,值1010
。 -
帖子或文章
UuidInterface::TYPE_POST
,值1020
。 -
新闻
UuidInterface::TYPE_NEWS
,值1021
。 -
图片
UuidInterface::TYPE_IMAGE
,值1030
。 -
图片相册
UuidInterface::TYPE_ALBUM
,值1031
。 -
评论
UuidInterface::TYPE_COMM
,值1040
。 -
评分
UuidInterface::TYPE_RATE
,值1041
。
变更日志
请参阅CHANGELOG以获取更多信息。
测试
$ composer test
贡献
请参阅CONTRIBUTE以获取更多信息。
依赖
-
PHP >= 5.4.0
-
phossa2/shared >= 2.0.21