Skip to content

Commit

Permalink
fix get null & timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
danilin-em committed Mar 27, 2024
1 parent 3c24ebc commit 6a7bc29
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Client/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
})(),
);
Expand Down

0 comments on commit 6a7bc29

Please sign in to comment.