Skip to content

Commit 1d31d46

Browse files
committed
Optimize Redis Cluster
1 parent eceb8dd commit 1d31d46

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/Dashboards/Redis/Compatibility/Cluster/RedisCluster.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -195,37 +195,40 @@ public function streamAdd(string $key, string $id, array $messages): string {
195195
* @param array<int, string> $keys
196196
*
197197
* @return array<string, mixed>
198-
*
199-
* @throws RedisClusterException
200198
*/
201199
public function pipelineKeys(array $keys): array {
202-
$lua_type = /* @lang Lua */
200+
$lua_get_key_info = /* @lang Lua */
203201
<<<LUA
204-
local type = redis.call("TYPE", KEYS[1])["ok"]
205-
if type == "set" then return redis.call("SCARD", KEYS[1])
206-
elseif type == "list" then return redis.call("LLEN", KEYS[1])
207-
elseif type == "zset" then return redis.call("ZCARD", KEYS[1])
208-
elseif type == "hash" then return redis.call("HLEN", KEYS[1])
209-
elseif type == "stream" then return redis.call("XLEN", KEYS[1])
210-
else return nil end
202+
local key = KEYS[1]
203+
local ttl = redis.call("TTL", key)
204+
local key_type = redis.call("TYPE", key)["ok"]
205+
local memory_usage = redis.call("MEMORY", "USAGE", key)
206+
local count = nil
207+
208+
if key_type == "set" then count = redis.call("SCARD", key)
209+
elseif key_type == "list" then count = redis.call("LLEN", key)
210+
elseif key_type == "zset" then count = redis.call("ZCARD", key)
211+
elseif key_type == "hash" then count = redis.call("HLEN", key)
212+
elseif key_type == "stream" then count = redis.call("XLEN", key)
213+
end
214+
215+
return {ttl, key_type, memory_usage, count}
211216
LUA;
212217

213218
$data = [];
214219

215220
foreach ($keys as $key) {
216-
// This must be per key because Redis Cluster doesn't support a pipeline.
217-
$pipe = $this->multi();
218-
$pipe->ttl($key);
219-
$pipe->type($key);
220-
$pipe->eval('return redis.call("MEMORY", "USAGE", KEYS[1])', [$key], 1);
221-
$pipe->eval($lua_type, [$key], 1);
222-
$results = $pipe->exec();
221+
$results = $this->eval($lua_get_key_info, [$key], 1);
222+
223+
if (!is_array($results) || count($results) < 3) {
224+
continue;
225+
}
223226

224227
$data[$key] = [
225228
'ttl' => $results[0],
226-
'type' => $this->getType($results[1]),
229+
'type' => $results[1],
227230
'size' => $results[2] ?? 0,
228-
'count' => is_int($results[3]) ? $results[3] : null,
231+
'count' => $results[3] ?? null,
229232
];
230233
}
231234

0 commit comments

Comments
 (0)