attla/ulid
Composer 安装命令:
composer require attla/ulid
包简介
Universally Unique Lexicographically Sortable Identifier (ULID) implementation for Attla and Laravel.
README 文档
README
A PHP port of ulid/javascript with some minor improvements.
Universally Unique Lexicographically Sortable Identifier
UUID can be suboptimal for many uses-cases because:
- It isn't the most character efficient way of encoding 128 bits of randomness
- UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
- UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
- UUID v4 provides no other information than randomness which can cause fragmentation in many data structures
Instead, herein is proposed ULID:
- 128-bit compatibility with UUID
- 1.21e+24 unique ULIDs per millisecond
- Lexicographically sortable!
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
- Case insensitive
- No special characters (URL safe)
- Monotonic sort order (correctly detects and handles the same millisecond)
You can read more here
What are the benefits?
- With distributed systems you can be pretty confident that the primary key’s will never collide.
- When building a large scale application when an auto increment primary key is not ideal.
- It makes replication trivial (as opposed to int’s, which makes it REALLY hard)
- Safe enough doesn’t show the user that you are getting information by id, for example
https://example.com/item/10
Installation
composer require attla/ulid
Usage
use Attla\Ulid\Factory as UlidFactory; $ulid = UlidFactory::generate(); echo $ulid; // 01B8KYR6G8BC61CE8R6K2T16HY echo $ulid->generate(); // 01B8KYR6G8BC61CE8R6K2T16HZ // Or if you prefer a lowercased output $ulid = UlidFactory::generate(true); echo $ulid->get(); // 01b8kyr6g8bc61ce8r6k2t16hy // If you need the timestamp from an ULID instance $ulid = UlidFactory::generate(); echo $ulid->toTimestamp(); // 1561622862 // You can also generate a ULID for a specific UNIX-time in milliseconds $ulid = UlidFactory::fromTimestamp(1593048767015); // or with a lower cased output: $ulid = UlidFactory::fromTimestamp(1593048767015, true); echo $ulid->toString(); // 01EBMHP6H7TT1Q4B7CA018K5MQ
Use the methods get() or toString() to get the ULID as a string on ULID instances.
Migrations
When using the migration you should change $table->increments('id') or $table->id() to:
$table->ulid();
Simply, the schema seems something like this.
Schema::create('items', function (Blueprint $table) { $table->ulid(); .... .... $table->timestamps(); });
If the related model is using an ULID, the column type should reflect that also.
Schema::create('items', function (Blueprint $table) { $table->ulid(); .... // related model that uses ULID $table->foreignUlid('category_id'); .... $table->timestamps(); });
The ULID blueprint parameter is optional. But below is an example of how to use it.
Schema::create('categories', function (Blueprint $table) { $table->ulid($ulidLength); .... // related model that uses ULID $table->foreignUlid($column, $foreignColumn, $foreignTable, $ulidLength); .... $table->timestamps(); });
Models
To set up a model to use ULID, simply use the HasUlid trait.
use Illuminate\Database\Eloquent\Model; use Attla\Ulid\HasUlid; class Item extends Model { use HasUlid; }
Controller
When you create a new instance of a model which uses ULIDs, this package will automatically add ULID as id of the model.
// 'HasUlid' trait will automatically generate and assign id field. $item = Item::create(['name' => 'Awesome item']); echo $item->id; // 01brh9q9amqp7mt7xqqb6b5k58
Testing
composer test
Benchmark
composer benchmark
License
This package is licensed under the MIT license © Octha.
attla/ulid 适用场景与选型建议
attla/ulid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 190 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 「laravel」 「ulid」 「attla」 「uuid-alternative」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 attla/ulid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 attla/ulid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 attla/ulid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
A simple drop-in solution for providing UUID and ULID support for the IDs of your Eloquent models.
Small Unique Identifier - Quite like an ULID, but half smaller (64 bits).
Provides Doctrine types for mediagone/small-uid package.
统计信息
- 总下载量: 190
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-02