This commit is contained in:
2020-09-08 17:58:54 +08:00
parent 0cc92bde43
commit 94093b5bc6
+9 -6
View File
@@ -295,12 +295,15 @@ class Redis extends Component
*/ */
public function lock($key, $timeout = 5) public function lock($key, $timeout = 5)
{ {
$redis = $this->proxy(); $script = <<<SCRIPT
if (!$redis->setnx($key, 1)) { local _nx = redis.call('setnx',KEYS[1], ARGV[1])
return false; if (_nx ~= 0) then
} redis.call('expire',KEYS[1], ARGV[1])
$redis->expire($key, $timeout); return 1
return true; end
return 0
SCRIPT;
return $this->proxy()->eval($script, [$key, $timeout], 1);
} }