diff --git a/src/Client/SocketClient.php b/src/Client/SocketClient.php index 538dd06..f770d54 100644 --- a/src/Client/SocketClient.php +++ b/src/Client/SocketClient.php @@ -18,7 +18,7 @@ final class SocketClient implements ClientInterface private string $address; private int $timeout; - public function __construct(string $address, int $timeout = 30) + public function __construct(string $address, int $timeout = 5) { $this->address = $address; $this->timeout = $timeout; @@ -39,6 +39,7 @@ public function connect(): void STREAM_CLIENT_CONNECT, $context ); + stream_set_timeout($this->socket, $this->timeout); if ($socket === false) { throw new ClientConnectionException( sprintf('Cannot establish connection: [%d] %s', $errorCode, $errorMessage) diff --git a/src/Memcached.php b/src/Memcached.php index e042b72..f1d5625 100644 --- a/src/Memcached.php +++ b/src/Memcached.php @@ -48,10 +48,13 @@ public function get(string $key): ?string } if (strpos($header, 'VALUE ') === 0) { [, , , $bytes] = explode(' ', trim($header)); - if (!$bytes) { + if (!is_numeric($bytes)) { throw new ResponseErrorException('Cannot parse header'); } - $data = $this->client->read((int)$bytes)->current(); + $data = null; + if ($bytes) { + $data = $this->client->read((int)$bytes)->current(); + } $line->next(); $end = $line->current(); if ($end !== null) { diff --git a/tests/Unit/MemcachedTest.php b/tests/Unit/MemcachedTest.php index 24c40ef..de3ae64 100644 --- a/tests/Unit/MemcachedTest.php +++ b/tests/Unit/MemcachedTest.php @@ -55,7 +55,7 @@ public function testGetReturnsValueWhenKeyExists(): void yield "VALUE key 0 5\r\n"; })(), (static function (): Generator { - yield "value\r\n"; + yield "value"; yield "END\r\n"; })(), );