justbetter/laravel-unique-values
Composer 安装命令:
composer require justbetter/laravel-unique-values
包简介
Package to generate unique values with support for concurrency utilizing cache locking
README 文档
README
Package to generate unique values
This package generates persistent unique values with support for concurrency utilizing scoped cache locking. It stores generated unique values in the database.
Generated values are unique per scope and can be generated using a callback method. This package only supports strings.
Features
- Generate unique values
- Support for concurrency
- Customizable generator
- Maximum attempts
- Support for subjects
Installation
You can install the package via composer:
composer require justbetter/laravel-unique-values
Usage
use JustBetter\UniqueValues\Support\UniqueValue; $uniqueValue = UniqueValue::make() ->scope('unique-scope') ->generator(function (int $attempt): string { return match ($attempt) { 0 => 'unique-value', default => 'unique-value-'.$attempt, }; })->generate();
Output after first run: unique-value.
Output after second run: unique-value-1.
Output after third run: unique-value-2.
Maximum attempts
You can configure the maximum attempts, this is three by default. If the maximum attempts are reached an exception is thrown.
use JustBetter\UniqueValues\Support\UniqueValue; $uniqueValue = UniqueValue::make() ->scope('unique-scope') ->attempts(2) ->generator(function (int $attempt): string { return match ($attempt) { 0 => 'unique-value', default => 'unique-value-'.$attempt, }; })->generate();
Subject
You can add a subject to retrieve a previously generated value for the subject. Optionally you can add an override flag to run the generation again for the subject.
use JustBetter\UniqueValues\Support\UniqueValue; $uniqueValue = UniqueValue::make() ->scope('unique-scope') ->subject('subject') ->generator(function (int $attempt): string { return match ($attempt) { 0 => 'unique-value', default => 'unique-value-'.$attempt, }; })->generate();
First run will output: unique-value.
Second run will output: unique-value.
Only one unique value will be stored in the database.
Quality
To ensure the quality of this package, run the following command:
composer quality
This will execute three tasks:
- Makes sure all tests are passed
- Checks for any issues using static code analysis
- Checks if the code is correctly formatted
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
justbetter/laravel-unique-values 适用场景与选型建议
justbetter/laravel-unique-values 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.05k 次下载、GitHub Stars 达 53, 最近一次更新时间为 2025 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 justbetter/laravel-unique-values 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 justbetter/laravel-unique-values 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 53
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-03-14