diff --git a/src/Connection/TcpConnection.php b/src/Connection/TcpConnection.php index 35654099f..29367918d 100644 --- a/src/Connection/TcpConnection.php +++ b/src/Connection/TcpConnection.php @@ -102,6 +102,20 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable */ public const STATUS_CLOSED = 8; + /** + * Maximum string length for cache + * + * @var int + */ + public const MAX_CACHE_STRING_LENGTH = 2048; + + /** + * Maximum cache size. + * + * @var int + */ + public const MAX_CACHE_SIZE = 512; + /** * Emitted when socket connection is successfully established. * @@ -635,7 +649,7 @@ public function baseRead($socket, bool $checkEof = true): void } else { $this->bytesRead += strlen($buffer); if ($this->recvBuffer === '') { - if (static::$enableCache && !isset($buffer[512]) && isset($requests[$buffer])) { + if (static::$enableCache && isset($requests[$buffer])) { ++self::$statistics['total_request']; $request = $requests[$buffer]; if ($request instanceof Request) { @@ -710,9 +724,9 @@ public function baseRead($socket, bool $checkEof = true): void /** @var ProtocolInterface $parser */ $parser = $this->protocol; $request = $parser::decode($oneRequestBuffer, $this); - if (static::$enableCache && (!is_object($request) || $request instanceof Request) && $one && !isset($oneRequestBuffer[512])) { + if (static::$enableCache && (!is_object($request) || $request instanceof Request) && $one && !isset($oneRequestBuffer[static::MAX_CACHE_STRING_LENGTH])) { $requests[$oneRequestBuffer] = $request; - if (count($requests) > 512) { + if (count($requests) > static::MAX_CACHE_SIZE) { unset($requests[key($requests)]); } } diff --git a/src/Protocols/Http.php b/src/Protocols/Http.php index c528ee08b..5eb3f0954 100644 --- a/src/Protocols/Http.php +++ b/src/Protocols/Http.php @@ -102,7 +102,7 @@ public static function enableCache(bool $value) public static function input(string $buffer, TcpConnection $connection): int { static $input = []; - if (!isset($buffer[512]) && isset($input[$buffer])) { + if (isset($input[$buffer])) { return $input[$buffer]; } $crlfPos = strpos($buffer, "\r\n\r\n"); @@ -148,9 +148,9 @@ public static function input(string $buffer, TcpConnection $connection): int return 0; } - if (!isset($buffer[512])) { + if (!isset($buffer[TcpConnection::MAX_CACHE_STRING_LENGTH])) { $input[$buffer] = $length; - if (count($input) > 512) { + if (count($input) > TcpConnection::MAX_CACHE_SIZE) { unset($input[key($input)]); } } @@ -168,8 +168,8 @@ public static function input(string $buffer, TcpConnection $connection): int public static function decode(string $buffer, TcpConnection $connection): Request { static $requests = []; - $cacheable = static::$enableCache && !isset($buffer[512]); - if (true === $cacheable && isset($requests[$buffer])) { + $cacheable = static::$enableCache && !isset($buffer[TcpConnection::MAX_CACHE_STRING_LENGTH]); + if (isset($requests[$buffer])) { $request = clone $requests[$buffer]; $request->connection = $connection; $connection->request = $request; @@ -181,7 +181,7 @@ public static function decode(string $buffer, TcpConnection $connection): Reques $connection->request = $request; if (true === $cacheable) { $requests[$buffer] = $request; - if (count($requests) > 512) { + if (count($requests) > TcpConnection::MAX_CACHE_SIZE) { unset($requests[key($requests)]); } }