Skip to content

Commit

Permalink
Fix memcache runCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNN1 committed Sep 12, 2023
1 parent fa6d3f7 commit 3d182ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/Dashboards/Memcached/Compatibility/MemcachedKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
23 changes: 4 additions & 19 deletions src/Dashboards/Memcached/Compatibility/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ trait RunCommand {
*
* @link https://github.com/memcached/memcached/blob/master/doc/protocol.txt
*
* @return array<int, mixed>|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)) {
Expand All @@ -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<int, mixed>|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);

Expand All @@ -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)) {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";}'],
];
Expand Down

0 comments on commit 3d182ba

Please sign in to comment.