ginnerpeace/laravel-redis-lock
Composer 安装命令:
composer require ginnerpeace/laravel-redis-lock
包简介
Simple redis distributed locks for Laravel.
关键字:
README 文档
README
Simple redis distributed locks for Laravel.
Getting started
Install
Using composer.
composer require "ginnerpeace/laravel-redis-lock:~2.3"
Add service provider:
Normally.
<?php return [ // .... 'providers' => [ // ... RedisLock\Providers\RedisLockServiceProvider::class, ], // Its optional. 'aliases' => [ // ... 'RedisLock' => RedisLock\Facades\RedisLock::class, ], // ... ];
After Laravel 5.5, the package auto-discovery is supported.
{ "providers": [ "RedisLock\\Providers\\RedisLockServiceProvider" ], "aliases": { "RedisLock": "RedisLock\\Facades\\RedisLock" } }
Lumen
$app->register(RedisLock\Providers\LumenRedisLockServiceProvider::class);
Publish resources (laravel only)
Copied config to
config/redislock.php.
php artisan vendor:publish --provider="RedisLock\Providers\RedisLockServiceProvider"
Default items:
<?php return [ // Use app('redis')->connection('default') 'connection' => 'default', 'retry_count' => 3, 'retry_delay' => 200, ];
Use
<?php use RedisLock\Facades\RedisLock; // Set the specified expire time, in milliseconds. $millisecond = 100000; // Try get lock. // If has non-null property `$this->retryCount`, will retry some times with its value. // Default value is `config('redislock.retry_count')` $payload = RedisLock::lock('key', $millisecond); /* [ "key" => "key", "token" => "21456004925bd1532e64616", "expire" => 100000, "expire_type" => "PX", ] */ // If cannot get lock, will return empty array. $payload = RedisLock::lock('key', 100000); /* [] */ // Return bool. RedisLock::unlock($payload); // Determine a lock if it still effective. RedisLock::verify($payload); // Reset a lock if it still effective. // The returned value is same to use RedisLock::lock() RedisLock::relock($payload, $millisecond); ///////////////////// // Special usages: // ///////////////////// // Retry 5 times when missing the first time. // Non-null `$retry` param will priority over `$this->retryCount`. RedisLock::lock('key', 100000, 5); // No retry (Try once only). RedisLock::lock('key', 100000, 0); // If value less than 0, still means try once only. // RedisLock::lock('key', 100000, -1); // Hmmmmmmm...Not pretty. // Change property `$this->retryDelay` (Default value is `config('redislock.retry_delay')`). // Retry 10 times when missing the first time. // Every retry be apart 500 ~ 1000 milliseconds. RedisLock::setRetryDelay(1000)->lock('key', 100000, 10); // PS: // RedisLock is default registered to singleton, call method `setRetryDelay()` will affects subsequent code. // Use in business logic: try { if (! $lock = RedisLock::lock('do-some-thing', 100000)) { throw new Exception('Resource locked.'); } ////////////////////// // Call ur methods. // ////////////////////// } catch (Exception $e) { throw $e; } finally { RedisLock::unlock($lock); }
ginnerpeace/laravel-redis-lock 适用场景与选型建议
ginnerpeace/laravel-redis-lock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 118.82k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2018 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「laravel」 「lumen」 「mutex lock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ginnerpeace/laravel-redis-lock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ginnerpeace/laravel-redis-lock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ginnerpeace/laravel-redis-lock 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
Microservice RPC through message queues.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Class to generate a standard structure for api json responses
统计信息
- 总下载量: 118.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-15