rawCommand('SET', self::getLockKey($key), $requestId, 'NX', 'EX', $lockSecond); if ($acquired) { break; } if ($timeout === 0) { break; } \Swoole\Coroutine\System::sleep($sleep); } while (!is_numeric($timeout) || (self::getMicroTime()) < ($start + ($timeout * 1000000))); return $acquired ? true : false; } /** * 释放锁 * * @param string $key 被加锁的KEY * @param string|int $requestId 客户端请求唯一ID * * @return bool */ public static function release(string $key, $requestId) { if (strlen($key) === 0) { return false; } $lua = <<rawCommand('eval', $lua, 1, self::getLockKey($key), $requestId); } /** * 获取锁 Key * * @param string $key 需要加锁的KEY * * @return string */ public static function getLockKey(string $key) { return self::PREFIX . ':' . $key; } /** * 获取当前微秒 * * @return string */ protected static function getMicroTime() { return bcmul(microtime(true), 1000000); } }