Skip to content

Commit de87a10

Browse files
committed
[feature] 使用set命令 替换setnx+expire
1 parent 6715a32 commit de87a10

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/RedisLock.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ public function __construct($redis, $name, $seconds, $owner = null)
2525
*/
2626
public function acquire()
2727
{
28-
$result = $this->redis->setnx($this->name, $this->owner);
29-
30-
if(intval($result) === 1 && $this->seconds > 0) {
31-
$this->redis->expire($this->name, $this->seconds);
28+
if ($this->seconds > 0) {
29+
$result = $this->redis->set($this->name, $this->owner,['nx', 'ex' => $this->seconds]);
30+
} else {
31+
$result = $this->redis->setnx($this->name, $this->owner);
32+
if(intval($result) === 1 && $this->seconds > 0) {
33+
$this->redis->expire($this->name, $this->seconds);
34+
}
3235
}
3336

3437
return intval($result) === 1;

0 commit comments

Comments
 (0)