Skip to content

Commit

Permalink
Add backward compatibility for older Memcached with lru_crawler dis…
Browse files Browse the repository at this point in the history
…abled
  • Loading branch information
RobiNN1 committed Feb 16, 2025
1 parent 16ce5d3 commit 3253af0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
13 changes: 10 additions & 3 deletions src/Dashboards/Memcached/MemcachedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private function keysTableView(array $keys): array {
'info' => [
'link_title' => urldecode($key_data['key']),
'bytes_size' => $key_data['size'],
'timediff_last_access' => $key_data['la'],
'timediff_last_access' => $key_data['la'] !== 0 ? $key_data['la'] : null,
'ttl' => $key_data['ttl'],
],
];
Expand All @@ -257,9 +257,16 @@ private function keysTableView(array $keys): array {
* @param array<int|string, mixed> $keys
*
* @return array<int, array<string, string|int>>
*
* @throws MemcachedException
*/
private function keysTreeView(array $keys): array {
$separator = urlencode($this->servers[$this->current_server]['separator'] ?? ':');
$separator = $this->servers[$this->current_server]['separator'] ?? ':';

if (version_compare($this->memcached->version(), '1.5.19', '>=')) {
$separator = urlencode($separator);
}

$this->template->addGlobal('separator', urldecode($separator));

$tree = [];
Expand All @@ -281,7 +288,7 @@ private function keysTreeView(array $keys): array {
'key' => $key_data['key'],
'info' => [
'bytes_size' => $key_data['size'],
'timediff_last_access' => $key_data['la'],
'timediff_last_access' => $key_data['la'] !== 0 ? $key_data['la'] : null,
'ttl' => $key_data['ttl'],
],
];
Expand Down
66 changes: 57 additions & 9 deletions src/Dashboards/Memcached/PHPMem.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,40 @@ public function isConnected(): bool {
* @throws MemcachedException
*/
public function getKeys(): array {
$raw = $this->runCommand('lru_crawler metadump all');
$lines = explode("\n", $raw);
array_pop($lines);
if (version_compare($this->version(), '1.5.19', '>=')) {
$raw = $this->runCommand('lru_crawler metadump all');
$lines = explode("\n", $raw);
array_pop($lines);

return $lines;
return $lines;
}

$slabs = $this->runCommand('stats items');
$lines = explode("\n", $slabs);
$slab_ids = [];

foreach ($lines as $line) {
if (preg_match('/STAT items:(\d+):/', $line, $matches)) {
$slab_ids[] = $matches[1];
}
}

$keys = [];

foreach (array_unique($slab_ids) as $slab_id) {
$dump = $this->runCommand('stats cachedump '.$slab_id.' 0');
$dump_lines = explode("\n", $dump);

foreach ($dump_lines as $line) {
if (preg_match('/ITEM (\S+) \[(\d+) b; (\d+) s\]/', $line, $matches)) {
$exp = (int) $matches[3] === 0 ? -1 : (int) $matches[3];
// Intentionally formatted as lru_crawler output
$keys[] = 'key='.$matches[1].' exp='.$exp.' la=0 cas=0 fetch=no cls=1 size='.$matches[2];
}
}
}

return $keys;
}

/**
Expand Down Expand Up @@ -166,15 +195,27 @@ public function getKey(string $key): string|false {
* @throws MemcachedException
*/
public function getKeyMeta(string $key): array {
$raw = $this->runCommand('me '.$key);
if (version_compare($this->version(), '1.5.19', '>=')) {
$raw = $this->runCommand('me '.$key);

if ($raw === 'ERROR') {
return [];
if ($raw === 'ERROR') {
return [];
}

$raw = preg_replace('/^ME\s+\S+\s+/', '', $raw); // Remove `ME keyname`

return $this->parseLine($raw);
}

$raw = preg_replace('/^ME\s+\S+\s+/', '', $raw); // Remove `ME keyname`
foreach ($this->getKeys() as $line) {
$data = $this->parseLine($line);

if ($data['key'] === $key) {
return $data;
}
}

return $this->parseLine($raw);
return [];
}

/**
Expand All @@ -186,6 +227,13 @@ public function exists(string $key): bool {
return $this->getKey($key) !== false;
}

/**
* @throws MemcachedException
*/
public function version(): string {
return str_replace('VERSION ', '', $this->runCommand('version'));
}

/**
* Run command.
*
Expand Down
2 changes: 1 addition & 1 deletion templates/partials/table_view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
{{- item|number -}}
{% elseif item_key starts with 'time_' %}
{{- item|time -}}
{% elseif item_key starts with 'timediff_' %}
{% elseif item_key starts with 'timediff_' and item is not empty %}
<span title="{{ item|time }}">{{- item|timediff -}}</span>
{% elseif item_key starts with 'bytes_' %}
{{- item|bytes -}}
Expand Down
2 changes: 1 addition & 1 deletion templates/partials/tree_view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{{- kitem|number -}}
{% elseif item_key starts with 'time_' %}
{{- kitem|time -}}
{% elseif item_key starts with 'timediff_' %}
{% elseif item_key starts with 'timediff_' and kitem is not empty %}
<span title="{{ kitem|time }}">{{- kitem|timediff -}}</span>
{% elseif item_key starts with 'bytes_' %}
{{- kitem|bytes -}}
Expand Down

0 comments on commit 3253af0

Please sign in to comment.