Skip to content

Commit 9866331

Browse files
committed
Fix memory issues
1 parent 790de59 commit 9866331

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -204,51 +204,51 @@ private function form(): string {
204204
}
205205

206206
/**
207-
* @return array<int, array<string, string|int>>
207+
* @return array<int, string>
208208
*
209209
* @throws MemcachedException
210210
*/
211211
public function getAllKeys(): array {
212212
$search = Http::get('s', '');
213213
$this->template->addGlobal('search_value', $search);
214214

215-
$all_keys = $this->memcached->getKeys();
216-
$keys = [];
217-
$time = time();
215+
$all_key_lines = $this->memcached->getKeys();
218216

219-
foreach ($all_keys as $key_data) {
220-
$key_data = $this->memcached->parseLine($key_data);
221-
$ttl = $key_data['exp'] ?? null;
217+
if ($search === '') {
218+
return $all_key_lines;
219+
}
222220

223-
if (stripos($key_data['key'], $search) !== false) {
224-
$keys[] = [
225-
'key' => $key_data['key'],
226-
'size' => $key_data['size'],
227-
'la' => $key_data['la'] ?? 0,
228-
'ttl' => $ttl === -1 ? 'Doesn\'t expire' : $ttl - $time,
229-
];
221+
$filtered_lines = [];
222+
foreach ($all_key_lines as $line) {
223+
if (preg_match('/key=(\S+)/', $line, $match) && stripos($match[1], $search) !== false) {
224+
$filtered_lines[] = $line;
230225
}
231226
}
232227

233-
return $keys;
228+
return $filtered_lines;
234229
}
235230

236231
/**
237-
* @param array<int|string, mixed> $keys
232+
* @param array<int, string> $raw_lines
238233
*
239-
* @return array<int, array<string, string|int>>
234+
* @return array<int, array<string, mixed>>
240235
*/
241-
public function keysTableView(array $keys): array {
236+
public function keysTableView(array $raw_lines): array {
242237
$formatted_keys = [];
238+
$time = time();
239+
240+
foreach ($raw_lines as $line) {
241+
$key_data = $this->memcached->parseLine($line);
242+
$ttl = $key_data['exp'] ?? null;
243+
$ttl_display = $ttl === -1 ? 'Doesn\'t expire' : $ttl - $time;
243244

244-
foreach ($keys as $key_data) {
245245
$formatted_keys[] = [
246246
'key' => $key_data['key'],
247247
'info' => [
248248
'link_title' => urldecode($key_data['key']),
249-
'bytes_size' => $key_data['size'],
250-
'timediff_last_access' => $key_data['la'] !== 0 ? $key_data['la'] : null,
251-
'ttl' => $key_data['ttl'],
249+
'bytes_size' => $key_data['size'] ?? 0,
250+
'timediff_last_access' => $key_data['la'] ?? 0,
251+
'ttl' => $ttl_display,
252252
],
253253
];
254254
}
@@ -257,13 +257,12 @@ public function keysTableView(array $keys): array {
257257
}
258258

259259
/**
260-
* @param array<int|string, mixed> $keys
261-
*
262-
* @return array<int, array<string, string|int>>
260+
* @param array<int, string> $raw_lines
263261
*
262+
* @return array<string, mixed>
264263
* @throws MemcachedException
265264
*/
266-
public function keysTreeView(array $keys): array {
265+
public function keysTreeView(array $raw_lines): array {
267266
$separator = $this->servers[$this->current_server]['separator'] ?? ':';
268267

269268
if (version_compare($this->memcached->version(), '1.5.19', '>=')) {
@@ -272,9 +271,20 @@ public function keysTreeView(array $keys): array {
272271

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

274+
$time = time();
275+
275276
$tree = [];
276277

277-
foreach ($keys as $key_data) {
278+
foreach ($raw_lines as $line) {
279+
$key_data = $this->memcached->parseLine($line);
280+
281+
if (!isset($key_data['key'])) {
282+
continue;
283+
}
284+
285+
$ttl = $key_data['exp'] ?? null;
286+
$ttl_display = $ttl === -1 ? 'Doesn\'t expire' : $ttl - $time;
287+
278288
$parts = explode($separator, $key_data['key']);
279289

280290
/** @var array<int|string, mixed> $current */
@@ -290,9 +300,9 @@ public function keysTreeView(array $keys): array {
290300
'name' => urldecode($part),
291301
'key' => $key_data['key'],
292302
'info' => [
293-
'bytes_size' => $key_data['size'],
294-
'timediff_last_access' => $key_data['la'] !== 0 ? $key_data['la'] : null,
295-
'ttl' => $key_data['ttl'],
303+
'bytes_size' => $key_data['size'] ?? 0,
304+
'timediff_last_access' => $key_data['la'] ?? 0,
305+
'ttl' => $ttl_display,
296306
],
297307
];
298308
} else {
@@ -322,7 +332,7 @@ public function keysTreeView(array $keys): array {
322332
* @return array<int|string, mixed>
323333
*/
324334
private function commandsStatsData(array $info): array {
325-
$rate = (static fn (int $hits, int $total): float => $hits !== 0 ? round(($hits / $total) * 100, 2) : 0);
335+
$rate = (static fn (int $hits, int $total): float => $hits !== 0 && $total !== 0 ? round(($hits / $total) * 100, 2) : 0);
326336

327337
$get_hit_rate = $rate($info['get_hits'], $info['cmd_get']);
328338
$delete_hit_rate = $rate($info['delete_hits'], $info['delete_hits'] + $info['delete_misses']);
@@ -526,23 +536,34 @@ private function mainDashboard(): string {
526536
return $this->metrics();
527537
}
528538

529-
$keys = $this->getAllKeys();
539+
$raw_key_lines = $this->getAllKeys();
530540

531541
if (isset($_GET['export_btn'])) {
532-
Helpers::export($keys, 'memcached_backup', function (string $key): ?string {
542+
$keys_to_export = [];
543+
foreach ($raw_key_lines as $line) {
544+
$key_data = $this->memcached->parseLine($line);
545+
if (isset($key_data['key'])) {
546+
$keys_to_export[] = [
547+
'key' => $key_data['key'],
548+
'ttl' => ($key_data['exp'] ?? -1) === -1 ? -1 : ($key_data['exp'] - time()),
549+
];
550+
}
551+
}
552+
553+
Helpers::export($keys_to_export, 'memcached_backup', function (string $key): ?string {
533554
$value = $this->memcached->getKey(urldecode($key));
534555

535556
return $value !== false ? base64_encode($value) : null;
536557
});
537558
}
538559

539-
$paginator = new Paginator($this->template, $keys);
540-
$paginated_keys = $paginator->getPaginated();
560+
$paginator = new Paginator($this->template, $raw_key_lines);
561+
$paginated_raw_lines = $paginator->getPaginated();
541562

542563
if (Http::get('view', Config::get('list-view', 'table')) === 'tree') {
543-
$keys_to_display = $this->keysTreeView($paginated_keys);
564+
$keys_to_display = $this->keysTreeView($paginated_raw_lines);
544565
} else {
545-
$keys_to_display = $this->keysTableView($paginated_keys);
566+
$keys_to_display = $this->keysTableView($paginated_raw_lines);
546567
}
547568

548569
return $this->template->render('dashboards/memcached/memcached', [

src/Paginator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
private int $per_page;
2222

2323
/**
24-
* @param array<int, array<string, int|string>> $items
25-
* @param array<int, array<int|string, string>> $url
24+
* @param array<int, string|array<string, int|string>> $items
25+
* @param array<int, array<int|string, string>> $url
2626
*/
2727
public function __construct(
2828
private Template $template,

0 commit comments

Comments
 (0)