Skip to content

Commit

Permalink
[feature] 使用set命令 替换setnx+expire
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysice committed Oct 18, 2021
1 parent 6715a32 commit de87a10
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/RedisLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ public function __construct($redis, $name, $seconds, $owner = null)
*/
public function acquire()
{
$result = $this->redis->setnx($this->name, $this->owner);

if(intval($result) === 1 && $this->seconds > 0) {
$this->redis->expire($this->name, $this->seconds);
if ($this->seconds > 0) {
$result = $this->redis->set($this->name, $this->owner,['nx', 'ex' => $this->seconds]);
} else {
$result = $this->redis->setnx($this->name, $this->owner);
if(intval($result) === 1 && $this->seconds > 0) {
$this->redis->expire($this->name, $this->seconds);
}
}

return intval($result) === 1;
Expand Down

0 comments on commit de87a10

Please sign in to comment.