Skip to content

Commit

Permalink
Memcached fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNN1 committed Oct 13, 2024
1 parent 549dd85 commit 3f6e12e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
49 changes: 21 additions & 28 deletions src/Dashboards/Memcached/Compatibility/MemcachedKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,35 @@ trait MemcachedKeys {
* @throws MemcachedException
*/
public function getKeys(): array {
$keys = [];

$raw = $this->runCommand('lru_crawler metadump all');
$lines = explode("\n", $raw);
array_pop($lines);
$lines = array_filter(explode("\n", trim($raw)), static fn ($line): bool => !empty($line) && $line !== 'END');
$keys = [];

foreach ($lines as $line) {
$keys[] = $this->keyData($line);
$keys[] = $this->parseLine($line);
}

return $keys;
}

/**
* Convert raw key line to an array.
*
* @return array<string, string|int>
*/
private function parseLine(string $line): array {
$data = [];

foreach (explode(' ', $line) as $part) {
if ($part !== '') {
[$key, $val] = explode('=', $part);
$data[$key] = is_numeric($val) ? (int) $val : $val;
}
}

return $data;
}

/**
* Get raw key.
*
Expand All @@ -65,27 +81,4 @@ public function getKey(string $key): string|false {
public function exists(string $key): bool {
return $this->getKey($key) !== false;
}

/**
* Convert raw key line to an array.
*
* @return array<string, string|int>
*/
private function keyData(string $line): array {
static $data = [];

foreach (explode(' ', $line) as $part) {
if ($part !== '') {
[$key, $val] = explode('=', $part);

if ($key === 'exp') {
$val = $val !== '-1' ? (int) $val - time() : (int) $val;
}

$data[$key] = $val;
}
}

return $data;
}
}
14 changes: 9 additions & 5 deletions src/Dashboards/Memcached/Compatibility/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ private function streamConnection(string $command, string $command_name): string
$start_time = time();

while (!feof($stream)) {
$buffer .= fgets($stream, 256);
$line = fgets($stream, 256);

if ($this->checkCommandEnd($command_name, $buffer) || time() - $start_time > 60) {
if ($line === false) {
break;
}

$buffer .= $line;

if ($this->checkCommandEnd($command_name, $buffer) || (time() - $start_time > 60)) {
break;
}
}
Expand All @@ -118,9 +124,7 @@ private function streamConnection(string $command, string $command_name): string
}

private function commandName(string $command): string {
$parts = explode(' ', $command);

return strtolower($parts[0]);
return strtolower(strtok($command, ' '));
}

private function checkCommandEnd(string $command, string $buffer): bool {
Expand Down
4 changes: 3 additions & 1 deletion src/Dashboards/Memcached/MemcachedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ private function getAllKeys(): array {

$this->template->addGlobal('search_value', $search);

$time = time();

foreach ($this->all_keys as $key_data) {
$key = $key_data['key'] ?? $key_data;

Expand All @@ -221,7 +223,7 @@ private function getAllKeys(): array {
'items' => [
'link_title' => $key,
'timediff_last_access' => $key_data['la'],
'ttl' => $ttl === -1 ? 'Doesn\'t expire' : $ttl,
'ttl' => $ttl === -1 ? 'Doesn\'t expire' : $ttl - $time,
],
];
}
Expand Down

0 comments on commit 3f6e12e

Please sign in to comment.