From 6a7bc292174abc60370b3f8680a47ab771d6e678 Mon Sep 17 00:00:00 2001 From: Danilin Evgenii Date: Wed, 27 Mar 2024 17:56:37 +0400 Subject: [PATCH] fix get null & timeout --- src/Client/SocketClient.php | 3 ++- src/Memcached.php | 7 +++++-- tests/Unit/MemcachedTest.php | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) 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"; })(), );