ex3v/random-string-key-generator
Composer 安装命令:
composer require ex3v/random-string-key-generator
包简介
Library to generate random and unique YouTube-like string ids, as an alternative to UUID
README 文档
README
Random String Key Generator
This little library makes it easy to generate short, unique, YouTube-like IDs that you can use instead of UUID in your application.
Using shorter (but still unique) ids will have positive inpact on database performance (especially MySQL, which don't handle UUID well).
This library consists of both generator itself, as well as Doctrine integration.
It allows you to customize length of default generator, as well as to write generators of your own.
Will it REALLY generate short unique ids?
Let's look at how many unique values can standard 8 characters long sequence have:
Example id:
9ZGpFadq
8 characters, each can have one of 64 values.
64^8 = 281 474 976 710 656 different values
In the end even UUID might not be unique, it depends on the way you see it. In the end, unless you're using really short ids, it's very close to impossible to get two duplicate strings. I ran multiple tests of generating large amounts of ids (tens of millions) in very short time and couldn't make it to get two duplicate ids.
If you are really afraid of duplicate values, you can extend the key length, but in general this is not neccesary.
1. Installation
Use Composer to install it:
composer require ex3v/random-string-key-generator
2. Usage
Generating ID manually
$factory = new GeneratorFactory(); $generator = $factory->getGenerator(); //will give you default generator with basic config $id = $generator->generateId();
Generating ID automatically in Doctrine
Just set up generator annotations (GeneratedValue(strategy="CUSTOM") and CustomIdGenerator(...)) on top of field declaration.
/** * @var string * * @ORM\Id() * @ORM\Column(name="id", type="string") * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class="Ex3v\RandomStringKeyGenerator\Doctrine\RandomStringKeyGenerator") */ private $id;
Changing default settings
This library comes with single generator, that returns 8 characters long strings.
Changing default key length and other default settings
//now default generator will return 12 characters long keys GeneratorFactory::setDefaultStrategy(UniqIdBasedGenerator::class, [GeneratorInterface::KEY_LENGTH => 12]);
Getting instance of your own generator
$factory = new GeneratorFactory(); $generator = $factory->getGenerator(YourGeneratorImplementingGeneratorInterface::class, ['generator' => 'options']);
I want to change default generator config for Doctrine and Symfony. How can I do that?
Unfortunately, Doctrine metadata factory restricts from passing any parameters or container parameters/services to ID Generators. Your only option here is to set GeneratorFactory defaults before app initialization. In case of Symfony, the place you're looking for is Kernel class.
3. Testing
Just run
composer test
in this repository, to run the test suite.
4. Contribution
Have idea on how to develop it? Found a bug and want to fix it? Pull Requests and Issues are welcome :)
ex3v/random-string-key-generator 适用场景与选型建议
ex3v/random-string-key-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.3k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「youtube」 「uuid」 「unique」 「keygen」 「key generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ex3v/random-string-key-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ex3v/random-string-key-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ex3v/random-string-key-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel queue connection that prevents identical jobs from being queued
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.
DrUUID RFC 4122 library for PHP
A Monolog processor to add X-Request-Id or UNIQUE_ID env to monolog stack if present, provide a default one otherwise. Optionally, add a forwarded request-id list.
Trait to generate uuid when creating models
统计信息
- 总下载量: 13.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-17