zilveer/laravel-redlock
Composer 安装命令:
composer require zilveer/laravel-redlock
包简介
Redis distributed locks for laravel
README 文档
README
Provides a generic locking mechanism using Redis. Implements the locking standard proposed by Redis.
Acknowledgements
This library was originally built by LibiChai based on the Redlock algorithm developed by antirez. The library was reworked by the team at That's Us, Inc.
Installation
composer require zilveer/laravel-redlock- Add
ThatsUs\RedLock\RedLockServiceProvider::class,to theprovidersarray in config/app.php - Enjoy!
It's Simple!
Set a lock on any scalar. If the lock() method returns false, you did not acquire the lock.
Store results of the lock() method. Use this value to release the lock with unlock().
Example
This example sets a lock on the key "1" with a 3 second expiration time.
If it acquired the lock, it does some work and releases the lock.
use ThatsUs\RedLock\Facades\RedLock; $product_id = 1; $lock_token = RedLock::lock($product_id, 3000); if ($lock_token) { $order->submit($product_id); RedLock::unlock($lock_token); }
Refresh
Use refreshLock() to reacquire and extend the time of your lock.
use ThatsUs\RedLock\Facades\RedLock; $product_ids = [1, 2, 3, 5, 7]; $lock_token = RedLock::lock('order-submitter', 3000); while ($product_ids && $lock_token) { $order->submit(array_shift($product_ids)); $lock_token = RedLock::refreshLock($lock_token); } RedLock::unlock($lock_token);
Even Easier with Closures
Use runLocked() for nicer syntax. The method returns the results of the closure, or else false if the lock could not be acquired.
use ThatsUs\RedLock\Facades\RedLock; $product_id = 1; $result = RedLock::runLocked($product_id, 3000, function () use ($order, $product_id) { $order->submit($product_id); return true; }); echo $result ? 'Worked!' : 'Lock not acquired.';
Refresh with Closures
You can easily refresh your tokens when using closures. The first parameter to your closure is $refresh. Simply call it when you want to refresh. If the lock cannot be refreshed, $refresh() will break out of the closure.
use ThatsUs\RedLock\Facades\RedLock; $product_ids = [1, 2, 3, 5, 7]; $result = RedLock::runLocked($product_id, 3000, function ($refresh) use ($order, $product_ids) { foreach ($product_ids as $product_id) { $refresh(); $order->submit($product_id); } return true; }); echo $result ? 'Worked!' : 'Lock lost or never acquired.';
Lock Queue Jobs Easily
If you're running jobs on a Laravel queue, you may want to avoid queuing up the same job more than once at a time.
The ThatsUs\RedLock\Traits\QueueWithoutOverlap trait provides this functionality with very few changes to your job. Usually only two changes are necessary.
use ThatsUs\RedLock\Traits\QueueWithoutOverlapas a trait- Change the
handle()method tohandleSync()
use ThatsUs\RedLock\Traits\QueueWithoutOverlap; class OrderProductJob { use QueueWithoutOverlap; public function __construct($order, $product_id) { $this->order = $order; $this->product_id = $product_id; } public function handleSync() { $this->order->submit($this->product_id); } }
Sometimes it's also necessary to specify a getLockKey() method. This method must return the string that needs to be locked.
This is typically unnecessary because the lock key can be generated automatically. But if the job's data is not easy to stringify, you must define the getLockKey() method.
This trait also provides a refresh method called refreshLock(). If refreshLock() is unable to refresh the lock, an exception is thrown and the job fails.
Finally, you can change the lock time-to-live from the default 300 seconds to another
value using the $lock_time property.
use ThatsUs\RedLock\Traits\QueueWithoutOverlap; class OrderProductsJob { use QueueWithoutOverlap; protected $lock_time = 600; // 10 minutes in seconds public function __construct($order, array $product_ids) { $this->order = $order; $this->product_ids = $product_ids; } // We need to define getLockKey() because $product_ids is an array and the // automatic key generator can't deal with arrays. protected function getLockKey() { $product_ids = implode(',', $this->product_ids); return "OrderProductsJob:{$this->order->id}:{$product_ids}"; } public function handleSync() { foreach ($this->product_ids as $product_id) { $this->refreshLock(); $this->order->submit($product_id); } } }
Contribution
If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.
License
zilveer/laravel-redlock 适用场景与选型建议
zilveer/laravel-redlock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 585 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redlock」 「laravel redis lock」 「redis lock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zilveer/laravel-redlock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zilveer/laravel-redlock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zilveer/laravel-redlock 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Redis distributed locks for laravel
Microservice RPC through message queues.
Redis distributed locks for laravel
The CodeIgniter Redis package
Symfony Lock Component
Asynchronous distributed locks with Redis and ReactPHP
统计信息
- 总下载量: 585
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-10