Cml\Lock\Redis::lock PHP Метод

lock() публичный Метод

上锁
public lock ( string $key, boolean $wouldBlock = false ) : mixed
$key string 要上的锁的key
$wouldBlock boolean 是否堵塞
Результат mixed
    public function lock($key, $wouldBlock = false)
    {
        if (empty($key)) {
            return false;
        }
        $key = $this->getKey($key);
        if (isset($this->lockCache[$key]) && $this->lockCache[$key] == Model::getInstance()->cache($this->useCache)->getInstance()->get($key)) {
            return true;
        }
        if (Model::getInstance()->cache($this->useCache)->getInstance()->set($key, Cml::$nowMicroTime, ['nx', 'ex' => $this->expire])) {
            $this->lockCache[$key] = (string) Cml::$nowMicroTime;
            return true;
        }
        //非堵塞模式
        if (!$wouldBlock) {
            return false;
        }
        //堵塞模式
        do {
            usleep(200);
        } while (!Model::getInstance()->cache($this->useCache)->getInstance()->set($key, Cml::$nowMicroTime, ['nx', 'ex' => $this->expire]));
        $this->lockCache[$key] = (string) Cml::$nowMicroTime;
        return true;
    }
Redis