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 基類來實現。