From 4dfef595db84cf0bb981a2182007bdad7fdf424e Mon Sep 17 00:00:00 2001 From: ironmanmi <38089200+ironmanmi@users.noreply.github.com> Date: Thu, 21 May 2020 19:38:19 +0800 Subject: [PATCH 1/2] Update RedisLock.php --- src/RedisLock.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/RedisLock.php b/src/RedisLock.php index 82758d7..d1a7c74 100644 --- a/src/RedisLock.php +++ b/src/RedisLock.php @@ -23,27 +23,26 @@ public function getLock($key, $timeout = NULL) $timeout = $timeout ? $timeout : $this->timeout; $lockCacheKey = $this->getLockCacheKey($key); $expireAt = time() + $timeout; + $isGet = (bool)$this->redis->setnx($lockCacheKey, $expireAt); if ($isGet) { return $expireAt; } - + while (1) { usleep(10); $time = time(); $oldExpire = $this->redis->get($lockCacheKey); if ($oldExpire >= $time) { continue; + } + $newExpire = $time + $timeout; $expireAt = $this->redis->getset($lockCacheKey, $newExpire); - if ($oldExpire != $expireAt) { - continue; - } - $isGet = $newExpire; - break; + return $expireAt; + } - return $isGet; } public function releaseLock($key, $newExpire) @@ -55,4 +54,4 @@ public function releaseLock($key, $newExpire) return true; } -} \ No newline at end of file +} From 0a0857c82ac3546236479533a09366fa50f93639 Mon Sep 17 00:00:00 2001 From: ironmanmi <38089200+ironmanmi@users.noreply.github.com> Date: Thu, 21 May 2020 19:41:29 +0800 Subject: [PATCH 2/2] Update RedisLock.php --- src/RedisLock.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/RedisLock.php b/src/RedisLock.php index d1a7c74..c848586 100644 --- a/src/RedisLock.php +++ b/src/RedisLock.php @@ -23,25 +23,20 @@ public function getLock($key, $timeout = NULL) $timeout = $timeout ? $timeout : $this->timeout; $lockCacheKey = $this->getLockCacheKey($key); $expireAt = time() + $timeout; - $isGet = (bool)$this->redis->setnx($lockCacheKey, $expireAt); if ($isGet) { return $expireAt; } - while (1) { usleep(10); $time = time(); $oldExpire = $this->redis->get($lockCacheKey); if ($oldExpire >= $time) { - continue; - + continue; } - $newExpire = $time + $timeout; $expireAt = $this->redis->getset($lockCacheKey, $newExpire); - return $expireAt; - + return $expireAt; } }