diff --git a/src/Dashboards/Memcached/Compatibility/MemcachedKeys.php b/src/Dashboards/Memcached/Compatibility/MemcachedKeys.php index 02c2adf..f60a61b 100644 --- a/src/Dashboards/Memcached/Compatibility/MemcachedKeys.php +++ b/src/Dashboards/Memcached/Compatibility/MemcachedKeys.php @@ -53,9 +53,10 @@ public function getKeys(): array { * @throws MemcachedException */ public function getKey(string $key) { - $data = $this->runCommand('get '.$key, true); + $raw = $this->runCommand('get '.$key); + $data = explode("\r\n", $raw); - if (!isset($data[0])) { + if ($data[0] === 'END') { return false; } diff --git a/src/Dashboards/Memcached/Compatibility/RunCommand.php b/src/Dashboards/Memcached/Compatibility/RunCommand.php index d7de416..64db3ad 100644 --- a/src/Dashboards/Memcached/Compatibility/RunCommand.php +++ b/src/Dashboards/Memcached/Compatibility/RunCommand.php @@ -77,11 +77,9 @@ trait RunCommand { * * @link https://github.com/memcached/memcached/blob/master/doc/protocol.txt * - * @return array|string - * * @throws MemcachedException */ - public function runCommand(string $command, bool $array = false) { + public function runCommand(string $command): string { $command_name = $this->commandName($command); if (!in_array($command_name, $this->allowed_commands, true)) { @@ -90,15 +88,13 @@ public function runCommand(string $command, bool $array = false) { $command = strtr($command, ['\r\n' => "\r\n"])."\r\n"; - return $this->streamConnection($command, $command_name, $array); + return $this->streamConnection($command, $command_name); } /** - * @return array|string - * * @throws MemcachedException */ - private function streamConnection(string $command, string $command_name, bool $array = false) { + private function streamConnection(string $command, string $command_name): string { $address = isset($this->server['path']) ? 'unix://'.$this->server['path'] : 'tcp://'.$this->server['host'].':'.$this->server['port']; $stream = @stream_socket_client($address, $error_code, $error_message, 3); @@ -109,7 +105,6 @@ private function streamConnection(string $command, string $command_name, bool $a fwrite($stream, $command, strlen($command)); $buffer = ''; - $data = []; $start_time = time(); while (!feof($stream)) { @@ -118,21 +113,11 @@ private function streamConnection(string $command, string $command_name, bool $a if ($this->checkCommandEnd($command_name, $buffer) || time() - $start_time > 60) { break; } - - // Bug fix for gzipped keys - if ($array) { - $lines = explode("\r\n", $buffer); - $buffer = array_pop($lines); - - foreach ($lines as $line) { - $data[] = trim($line); - } - } } fclose($stream); - return $array ? $data : rtrim($buffer, "\r\n"); + return rtrim($buffer, "\r\n"); } private function commandName(string $command): string { diff --git a/tests/TestCase.php b/tests/TestCase.php index 4875055..5bcad88 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,7 +23,9 @@ public static function keysProvider(): array { ['float', 23.99, '23.99'], ['bool', true, '1'], ['null', null, ''], - ['gzip', gzcompress('test'), gzcompress('test')], + ['gzcompress', gzcompress('test'), gzcompress('test')], + ['gzencode', gzencode('test'), gzencode('test')], + ['gzdeflate', gzdeflate('test'), gzdeflate('test')], ['array', ['key1', 'key2'], 'a:2:{i:0;s:4:"key1";i:1;s:4:"key2";}'], ['object', (object) ['key1', 'key2'], 'O:8:"stdClass":2:{s:1:"0";s:4:"key1";s:1:"1";s:4:"key2";}'], ];