potsky / laravel-redlock
为 Laravel 提供的 Redis 分布式锁
Requires
- php: >=5.4.0
- illuminate/console: ^5.0
- illuminate/support: ^5.0
- predis/predis: ^1.1
Requires (Dev)
- laravel/framework: ^5
- php-mock/php-mock-mockery: ^1.1
- phpunit/phpunit: ~5|^6
This package is not auto-updated.
Last update: 2024-09-15 05:13:11 UTC
README
提供使用 Redis 的通用锁定机制。实现了 Redis 提出的锁定标准。
致谢
这个库最初由 LibiChai 基于 antirez 开发的 Redlock 算法构建。该库被 That's Us, Inc. 团队重构。
安装
composer require thatsus/laravel-redlock
- 将
ThatsUs\RedLock\RedLockServiceProvider::class,
添加到 config/app.php 中的providers
数组 - 享受吧!
很简单!
对任何标量设置锁。如果 lock()
方法返回 false,则表示未获取到锁。
存储 lock()
方法的返回结果。使用此值通过 unlock()
释放锁。
示例
此示例将对键 "1" 设置一个 3 秒的过期时间锁。
如果获取了锁,它将执行一些工作并释放锁。
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); }
刷新
使用 refreshLock()
重新获取并延长锁的时间。
use ThatsUs\RedLock\Facades\RedLock; $product_ids = [1, 2, 3, 5, 7]; $lock_token = RedLock::lock('order-submitter', 3000); while ($lock_token) { $order->submit(array_shift($product_ids)); $lock_token = RedLock::refreshLock($lock_token); } RedLock::unlock($lock_token);
使用闭包更简单
使用 runLocked()
获取更简洁的语法。该方法返回闭包的结果,或者如果无法获取锁则返回 false。
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
。当您需要刷新时调用它。如果无法刷新锁,则闭包将返回。
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.';
轻松锁定队列任务
如果您在 Laravel 队列中运行作业,可能希望避免同时排队相同的作业。
ThatsUs\RedLock\Traits\QueueWithoutOverlap
trait 通过对您的作业进行很少的修改提供了此功能。通常只需要两个修改。
- 使用
use ThatsUs\RedLock\Traits\QueueWithoutOverlap
作为 trait - 将
handle()
方法更改为handleSync()
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); } }
有时还需要指定一个 getLockKey()
方法。此方法必须返回需要被锁定的字符串。
通常这并不必要,因为锁键可以自动生成。但如果作业的数据不容易转换为字符串,则必须定义 getLockKey()
方法。
该 trait 还提供了一个名为 refreshLock()
的刷新方法。如果 refreshLock()
无法刷新锁,则会抛出异常并使作业失败。
最后,您可以使用 $lock_time
属性将锁的超时时间从默认的 300 秒更改为其他值。
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); } } }