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;
}