think.studio / laravel-temporal-key
在数据库中创建和检索随机字符串键,用于任何目的。
1.1.0
2023-08-20 06:21 UTC
Requires
- php: ^8.1
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.1
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.11
README
为任何目的在数据库中创建临时随机字符串键。键将在过期时间或最大检索尝试次数后删除。
安装
通过composer安装此包
composer require think.studio/laravel-temporal-key
可选地,您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="TemporalKey\ServiceProvider" --tag="config"
迁移数据库
php artisan migrate
将prune命令添加到控制台内核
$schedule->command('temporal-key:prune')->everyTenMinutes();
用法
默认用法
创建键
$key = \TemporalKey\Manager\TmpKey::create()->key() // Customize default expiration datetime $key = \TemporalKey\Manager\TmpKey::create(validUntil: \Carbon\Carbon::now()->addDay())->key() // Add metadata $key = \TemporalKey\Manager\TmpKey::create(['email' => 'user@email.com'])->key() // Customise custom maximal retrieve count. $key = \TemporalKey\Manager\TmpKey::create(usageMax: 22)->key()
检索键
$temporalKey = \TemporalKey\Manager\TmpKey::find('testkey'); $temporalKey?->key(); $temporalKey?->meta();
创建自定义键类型
use TemporalKey\Manager\TmpKey; class ImagePreviewTmpKey extends TmpKey { public static string $type = 'image-preview'; public static int $defaultValidSeconds = 60 * 60; public static int $defaultUsageMax = 0; // unlimited } $key = ImagePreviewTmpKey::create()->key() $temporalKey = ImagePreviewTmpKey::find('testkey');