seconds > 0) { return $this->redis->set($this->name, $this->owner, ['nx', 'ex' => $this->seconds]) == true; } else { return $this->redis->setnx($this->name, $this->owner) == true; } } /** * Release the lock. * * @return bool */ public function release() { return (bool) $this->redis->eval(LuaScripts::releaseLock(), [$this->name, $this->owner], 1); } /** * @throws LockFailException */ public static function lockOrFail($name, $seconds, $owner = null): NexusLock { $lock = new self($name, $seconds, $owner); if (!$lock->acquire()) { do_log("$name failed to acquire lock", 'error'); throw new LockFailException($name, $lock->owner()); } return $lock; } }