RedisLock
RedisLock
Laravel 内部实现了一个 RedisLock 功能,代码相对简单,也不太严谨,对锁那么高要求的应用可以使用。
class RedisLock extends Lock
{
/**
* The Redis factory implementation.
*
* @var \Illuminate\Redis\Connections\Connection
*/
protected $redis;
/**
* Create a new lock instance.
*
* @param \Illuminate\Redis\Connections\Connection $redis
* @param string $name
* @param int $seconds
* @return void
*/
public function __construct($redis, $name, $seconds)
{
parent::__construct($name, $seconds);
$this->redis = $redis;
}
/**
* Attempt to acquire the lock.
*
* @return bool
*/
public function acquire()
{
$result = $this->redis->setnx($this->name, 1);
if ($result === 1 && $this->seconds > 0) {
$this->redis->expire($this->name, $this->seconds);
}
return $result === 1;
}
/**
* Release the lock.
*
* @return void
*/
public function release()
{
$this->redis->del($this->name);
}
}
RedisLock 继承与 Lock 基类,Lock 中也实现了一些非常实用的方法:
// 如果获取到锁,则执行 $callback 回调
public function get($callback = null)
{
$result = $this->acquire();
if ($result && is_callable($callback)) {
return tap($callback(), function () {
$this->release();
});
}
return $result;
}
// 如果获取到锁,则执行 $callback 回调
// 如果没有获取到锁,会等待250毫秒,继续去获取锁
// 如果在 $seconds 秒之内还没有获取到锁,会抛出 LockTimeoutException 异常
public function block($seconds, $callback = null)
{
$starting = $this->currentTime();
while (! $this->acquire()) {
usleep(250 * 1000);
if ($this->currentTime() - $seconds >= $starting) {
throw new LockTimeoutException;
}
}
if (is_callable($callback)) {
return tap($callback(), function () {
$this->release();
});
}
return true;
}
假如想实现自己的分布式锁(比如利用 Zookeeper),也可以继承于 Lock 基类来实现。
本作品采用《CC 协议》,转载必须注明作者和本文链接
redis 有setex 不用setnx expire