Skip to content

Commit c8f76a8

Browse files
committed
Show redis keys size in table
1 parent e986621 commit c8f76a8

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,28 @@ public function pipelineKeys(array $keys): array {
135135
foreach ($keys as $key) {
136136
$pipe->ttl($key);
137137
$pipe->type($key);
138+
$pipe->eval('return redis.call("MEMORY", "USAGE", KEYS[1])', 1, $key);
138139
}
139140
});
140141

141142
$data = [];
142143

143144
foreach ($keys as $i => $key) {
145+
$index = $i * 3;
146+
144147
$data[$key] = [
145-
'ttl' => $results[$i * 2],
146-
'type' => $this->getType((string) $results[$i * 2 + 1]),
148+
'ttl' => $results[$index],
149+
'type' => $this->getType((string) $results[$index + 1]),
150+
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
147151
];
148152
}
149153

150154
return $data;
151155
}
156+
157+
public function size(string $key): int {
158+
$size = $this->executeRaw(['MEMORY', 'USAGE', $key]);
159+
160+
return is_int($size) ? $size : 0;
161+
}
152162
}

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,29 @@ public function pipelineKeys(array $keys): array {
154154
foreach ($keys as $key) {
155155
$pipeline->ttl($key);
156156
$pipeline->type($key);
157+
$pipeline->rawcommand('MEMORY', 'USAGE', $key);
157158
}
158159

159160
$results = $pipeline->exec();
160161

161162
$data = [];
162163

163164
foreach ($keys as $i => $key) {
165+
$index = $i * 3;
166+
164167
$data[$key] = [
165-
'ttl' => $results[$i * 2],
166-
'type' => $this->getType($results[$i * 2 + 1]),
168+
'ttl' => $results[$index],
169+
'type' => $this->getType($results[$index + 1]),
170+
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
167171
];
168172
}
169173

170174
return $data;
171175
}
176+
177+
public function size(string $key): int {
178+
$size = $this->rawcommand('MEMORY', 'USAGE', $key);
179+
180+
return is_int($size) ? $size : 0;
181+
}
172182
}

src/Dashboards/Redis/Compatibility/RedisCompatibilityInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ public function streamAdd(string $key, string $id, array $messages): string;
5757
* @return array<string, mixed>
5858
*/
5959
public function pipelineKeys(array $keys): array;
60+
61+
62+
/**
63+
* Get key size.
64+
*
65+
* Requires Redis >= 4.0.0.
66+
*/
67+
public function size(string $key): int;
6068
}

src/Dashboards/Redis/RedisTrait.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,12 @@ private function viewKey(): string {
220220
[$value, $encode_fn, $is_formatted] = Value::format($value);
221221
}
222222

223-
224-
$size = $this->redis->rawCommand('MEMORY', 'usage', $key); // requires Redis >= 4.0.0
225-
226223
return $this->template->render('partials/view_key', [
227224
'key' => $key,
228225
'value' => $value,
229226
'type' => $type,
230227
'ttl' => Format::seconds($ttl),
231-
'size' => is_int($size) ? Format::bytes($size) : null,
228+
'size' => Format::bytes($this->redis->size($key)),
232229
'encode_fn' => $encode_fn,
233230
'formatted' => $is_formatted,
234231
'add_subkey_url' => Http::queryString(['db'], ['form' => 'new', 'key' => $key]),
@@ -354,6 +351,7 @@ private function getAllKeys(): array {
354351
'base64' => true,
355352
'items' => [
356353
'link_title' => ($total !== null ? '('.$total.' items) ' : '').$key,
354+
'bytes_size' => $pipeline[$key]['size'],
357355
'type' => $type,
358356
'ttl' => $ttl === -1 ? 'Doesn\'t expire' : $ttl,
359357
],

templates/dashboards/redis/redis.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
{{ include('partials/keys_table.twig', {
2020
head_items: [
2121
{'title': 'Key'},
22+
{'title': 'Size', 'class': 'w-16'},
2223
{'title': 'Type', 'class': 'w-24 hidden md:table-cell'},
2324
{'title': 'TTL', 'class': 'w-32'},
2425
],

0 commit comments

Comments
 (0)