Skip to content

Commit 0497362

Browse files
committed
Optimize Redis
1 parent 05380fa commit 0497362

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,46 +131,40 @@ public function rawcommand(string $command, mixed ...$arguments): mixed {
131131
* @return array<string, mixed>
132132
*/
133133
public function pipelineKeys(array $keys): array {
134-
$results = $this->pipeline(function ($pipe) use ($keys): void {
134+
$lua_memory = 'return redis.call("MEMORY", "USAGE", KEYS[1])';
135+
$lua_type = /* @lang Lua */
136+
<<<LUA
137+
local type = redis.call("TYPE", KEYS[1])["ok"]
138+
if type == "set" then return redis.call("SCARD", KEYS[1])
139+
elseif type == "list" then return redis.call("LLEN", KEYS[1])
140+
elseif type == "zset" then return redis.call("ZCARD", KEYS[1])
141+
elseif type == "hash" then return redis.call("HLEN", KEYS[1])
142+
elseif type == "stream" then return redis.call("XLEN", KEYS[1])
143+
else return nil end
144+
LUA;
145+
146+
$results = $this->pipeline(function ($pipe) use ($keys, $lua_memory, $lua_type): void {
135147
foreach ($keys as $key) {
136148
$pipe->ttl($key);
137149
$pipe->type($key);
138-
$pipe->eval('return redis.call("MEMORY", "USAGE", KEYS[1])', 1, $key);
150+
$pipe->eval($lua_memory, 1, $key);
151+
$pipe->eval($lua_type, 1, $key);
139152
}
140153
});
141154

142155
$data = [];
143156

144157
foreach ($keys as $i => $key) {
145-
$index = $i * 3;
158+
$index = $i * 4;
159+
146160
$data[$key] = [
147161
'ttl' => $results[$index],
148162
'type' => (string) $results[$index + 1],
149-
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
150-
'count' => null,
163+
'size' => $results[$index + 2] ?? 0,
164+
'count' => is_int($results[$index + 3]) ? $results[$index + 3] : null,
151165
];
152166
}
153167

154-
$count_results = $this->pipeline(function ($pipe) use ($keys, $data): void {
155-
foreach ($keys as $key) {
156-
$lua = match ($data[$key]['type']) {
157-
'set' => 'return redis.call("SCARD", KEYS[1])',
158-
'list' => 'return redis.call("LLEN", KEYS[1])',
159-
'zset' => 'return redis.call("ZCARD", KEYS[1])',
160-
'hash' => 'return redis.call("HLEN", KEYS[1])',
161-
'stream' => 'return redis.call("XLEN", KEYS[1])',
162-
default => 'return nil',
163-
};
164-
165-
$pipe->eval($lua, 1, $key);
166-
}
167-
});
168-
169-
foreach ($keys as $i => $key) {
170-
$count = $count_results[$i] ?? null;
171-
$data[$key]['count'] = $data[$key]['type'] !== 'none' && is_int($count) ? $count : null;
172-
}
173-
174168
return $data;
175169
}
176170

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,25 @@ public function streamAdd(string $key, string $id, array $messages): string {
149149
* @throws RedisException
150150
*/
151151
public function pipelineKeys(array $keys): array {
152-
$pipeline = $this->multi(self::PIPELINE);
152+
$pipe = $this->multi(self::PIPELINE);
153153

154154
foreach ($keys as $key) {
155-
$pipeline->ttl($key);
156-
$pipeline->type($key);
157-
$pipeline->rawcommand('MEMORY', 'USAGE', $key);
158-
$pipeline->rawcommand('SCARD', $key);
159-
$pipeline->rawcommand('LLEN', $key);
160-
$pipeline->rawcommand('ZCARD', $key);
161-
$pipeline->rawcommand('HLEN', $key);
162-
$pipeline->rawcommand('XLEN', $key);
155+
$pipe->ttl($key);
156+
$pipe->type($key);
157+
$pipe->rawcommand('MEMORY', 'USAGE', $key);
158+
$pipe->rawcommand('SCARD', $key);
159+
$pipe->rawcommand('LLEN', $key);
160+
$pipe->rawcommand('ZCARD', $key);
161+
$pipe->rawcommand('HLEN', $key);
162+
$pipe->rawcommand('XLEN', $key);
163163
}
164164

165-
$results = $pipeline->exec();
165+
$results = $pipe->exec();
166166

167167
$data = [];
168168

169169
foreach ($keys as $i => $key) {
170-
$index = $i * 8;
170+
$index = $i * 8; // index + count of pipeline commands
171171
$type = $this->getType($results[$index + 1]);
172172

173173
$count = match ($type) {

0 commit comments

Comments
 (0)