/**
* @param $key
* @param int $expireTime
* @return bool|int
*/
public function lock($key, $expireTime = 5)
{
$key = 'get_lock_' . $key;
$isLock = $this->connection->setnx($key, Carbon::now()->timestamp + $expireTime);
if ($isLock) {
return true;
} else {
$val = $this->connection->get($key);
if ($val && $val < Carbon::now()->timestamp) {
$this->unLock($key);
}
return $this->connection->setnx($key, Carbon::now()->timestamp + $expireTime);
}
}
/**
* @param $key
*/
public function unLock($key)
{
$key = 'get_lock_' . $key;
$this->connection->del([$key]);
}
如何利用Redis锁解决高并发问题?
最新推荐文章于 2024-05-05 12:00:36 发布