Skip to content

Commit 3253af0

Browse files
committed
Add backward compatibility for older Memcached with lru_crawler disabled
1 parent 16ce5d3 commit 3253af0

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function keysTableView(array $keys): array {
244244
'info' => [
245245
'link_title' => urldecode($key_data['key']),
246246
'bytes_size' => $key_data['size'],
247-
'timediff_last_access' => $key_data['la'],
247+
'timediff_last_access' => $key_data['la'] !== 0 ? $key_data['la'] : null,
248248
'ttl' => $key_data['ttl'],
249249
],
250250
];
@@ -257,9 +257,16 @@ private function keysTableView(array $keys): array {
257257
* @param array<int|string, mixed> $keys
258258
*
259259
* @return array<int, array<string, string|int>>
260+
*
261+
* @throws MemcachedException
260262
*/
261263
private function keysTreeView(array $keys): array {
262-
$separator = urlencode($this->servers[$this->current_server]['separator'] ?? ':');
264+
$separator = $this->servers[$this->current_server]['separator'] ?? ':';
265+
266+
if (version_compare($this->memcached->version(), '1.5.19', '>=')) {
267+
$separator = urlencode($separator);
268+
}
269+
263270
$this->template->addGlobal('separator', urldecode($separator));
264271

265272
$tree = [];
@@ -281,7 +288,7 @@ private function keysTreeView(array $keys): array {
281288
'key' => $key_data['key'],
282289
'info' => [
283290
'bytes_size' => $key_data['size'],
284-
'timediff_last_access' => $key_data['la'],
291+
'timediff_last_access' => $key_data['la'] !== 0 ? $key_data['la'] : null,
285292
'ttl' => $key_data['ttl'],
286293
],
287294
];

src/Dashboards/Memcached/PHPMem.php

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,40 @@ public function isConnected(): bool {
113113
* @throws MemcachedException
114114
*/
115115
public function getKeys(): array {
116-
$raw = $this->runCommand('lru_crawler metadump all');
117-
$lines = explode("\n", $raw);
118-
array_pop($lines);
116+
if (version_compare($this->version(), '1.5.19', '>=')) {
117+
$raw = $this->runCommand('lru_crawler metadump all');
118+
$lines = explode("\n", $raw);
119+
array_pop($lines);
119120

120-
return $lines;
121+
return $lines;
122+
}
123+
124+
$slabs = $this->runCommand('stats items');
125+
$lines = explode("\n", $slabs);
126+
$slab_ids = [];
127+
128+
foreach ($lines as $line) {
129+
if (preg_match('/STAT items:(\d+):/', $line, $matches)) {
130+
$slab_ids[] = $matches[1];
131+
}
132+
}
133+
134+
$keys = [];
135+
136+
foreach (array_unique($slab_ids) as $slab_id) {
137+
$dump = $this->runCommand('stats cachedump '.$slab_id.' 0');
138+
$dump_lines = explode("\n", $dump);
139+
140+
foreach ($dump_lines as $line) {
141+
if (preg_match('/ITEM (\S+) \[(\d+) b; (\d+) s\]/', $line, $matches)) {
142+
$exp = (int) $matches[3] === 0 ? -1 : (int) $matches[3];
143+
// Intentionally formatted as lru_crawler output
144+
$keys[] = 'key='.$matches[1].' exp='.$exp.' la=0 cas=0 fetch=no cls=1 size='.$matches[2];
145+
}
146+
}
147+
}
148+
149+
return $keys;
121150
}
122151

123152
/**
@@ -166,15 +195,27 @@ public function getKey(string $key): string|false {
166195
* @throws MemcachedException
167196
*/
168197
public function getKeyMeta(string $key): array {
169-
$raw = $this->runCommand('me '.$key);
198+
if (version_compare($this->version(), '1.5.19', '>=')) {
199+
$raw = $this->runCommand('me '.$key);
170200

171-
if ($raw === 'ERROR') {
172-
return [];
201+
if ($raw === 'ERROR') {
202+
return [];
203+
}
204+
205+
$raw = preg_replace('/^ME\s+\S+\s+/', '', $raw); // Remove `ME keyname`
206+
207+
return $this->parseLine($raw);
173208
}
174209

175-
$raw = preg_replace('/^ME\s+\S+\s+/', '', $raw); // Remove `ME keyname`
210+
foreach ($this->getKeys() as $line) {
211+
$data = $this->parseLine($line);
212+
213+
if ($data['key'] === $key) {
214+
return $data;
215+
}
216+
}
176217

177-
return $this->parseLine($raw);
218+
return [];
178219
}
179220

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

230+
/**
231+
* @throws MemcachedException
232+
*/
233+
public function version(): string {
234+
return str_replace('VERSION ', '', $this->runCommand('version'));
235+
}
236+
189237
/**
190238
* Run command.
191239
*

templates/partials/table_view.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
{{- item|number -}}
4545
{% elseif item_key starts with 'time_' %}
4646
{{- item|time -}}
47-
{% elseif item_key starts with 'timediff_' %}
47+
{% elseif item_key starts with 'timediff_' and item is not empty %}
4848
<span title="{{ item|time }}">{{- item|timediff -}}</span>
4949
{% elseif item_key starts with 'bytes_' %}
5050
{{- item|bytes -}}

templates/partials/tree_view.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
{{- kitem|number -}}
4040
{% elseif item_key starts with 'time_' %}
4141
{{- kitem|time -}}
42-
{% elseif item_key starts with 'timediff_' %}
42+
{% elseif item_key starts with 'timediff_' and kitem is not empty %}
4343
<span title="{{ kitem|time }}">{{- kitem|timediff -}}</span>
4444
{% elseif item_key starts with 'bytes_' %}
4545
{{- kitem|bytes -}}

0 commit comments

Comments
 (0)