Skip to content

Commit 53a775a

Browse files
committed
Fix Redis errors if MEMORY command is disabled
1 parent a13578e commit 53a775a

File tree

7 files changed

+62
-15
lines changed

7 files changed

+62
-15
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function streamAdd(string $key, string $id, array $messages): string {
197197
}
198198

199199
public function rawcommand(string $command, mixed ...$arguments): mixed {
200-
return $this->executeRaw(func_get_args());
200+
return $this->nodes[0]->executeRaw(func_get_args());
201201
}
202202

203203
/**
@@ -328,4 +328,16 @@ public function resetSlowlog(): bool {
328328

329329
return true;
330330
}
331+
332+
public function isCommandSupported(string $command): bool {
333+
try {
334+
$commands = $this->nodes[0]->executeRaw(['COMMAND']);
335+
$command_names = array_column($commands, 0);
336+
$is_supported = in_array(strtolower($command), $command_names, true);
337+
} catch (Exception) {
338+
$is_supported = false;
339+
}
340+
341+
return $is_supported;
342+
}
331343
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,16 @@ public function resetSlowlog(): bool {
333333

334334
return true;
335335
}
336+
337+
public function isCommandSupported(string $command): bool {
338+
try {
339+
$commands = $this->rawcommand($this->nodes[0], 'COMMAND');
340+
$command_names = array_column($commands, 0);
341+
$is_supported = in_array(strtolower($command), $command_names, true);
342+
} catch (RedisClusterException) {
343+
$is_supported = false;
344+
}
345+
346+
return $is_supported;
347+
}
336348
}

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace RobiNN\Pca\Dashboards\Redis\Compatibility;
1010

11+
use Exception;
1112
use Predis\Client;
1213
use Predis\Collection\Iterator\Keyspace;
1314

@@ -197,4 +198,16 @@ public function getSlowlog(int $count): ?array {
197198
public function resetSlowlog(): bool {
198199
return $this->slowlog('RESET') === 'OK';
199200
}
201+
202+
public function isCommandSupported(string $command): bool {
203+
try {
204+
$commands = $this->rawcommand('COMMAND');
205+
$command_names = array_column($commands, 0);
206+
$is_supported = in_array(strtolower($command), $command_names, true);
207+
} catch (Exception) {
208+
$is_supported = false;
209+
}
210+
211+
return $is_supported;
212+
}
200213
}

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,16 @@ public function getSlowlog(int $count): ?array {
213213
public function resetSlowlog(): bool {
214214
return $this->slowlog('RESET');
215215
}
216+
217+
public function isCommandSupported(string $command): bool {
218+
try {
219+
$commands = $this->rawcommand('COMMAND');
220+
$command_names = array_column($commands, 0);
221+
$is_supported = in_array(strtolower($command), $command_names, true);
222+
} catch (RedisException) {
223+
$is_supported = false;
224+
}
225+
226+
return $is_supported;
227+
}
216228
}

src/Dashboards/Redis/Compatibility/RedisCompatibilityInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,9 @@ public function getSlowlog(int $count): ?array;
9191
* Reset Slowlog.
9292
*/
9393
public function resetSlowlog(): bool;
94+
95+
/**
96+
* Check if a command is supported.
97+
*/
98+
public function isCommandSupported(string $command): bool;
9499
}

src/Dashboards/Redis/Compatibility/get_key_info.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
local key = KEYS[1]
22
local ttl = redis.call("TTL", key)
33
local key_type = redis.call("TYPE", key)["ok"]
4-
local memory_usage = redis.call("MEMORY", "USAGE", key)
4+
5+
local ok, mem = pcall(function()
6+
return redis.call("MEMORY", "USAGE", key)
7+
end)
8+
local memory_usage = ok and mem or 0
9+
510
local count
611

712
if key_type == "set" then

src/Dashboards/Redis/RedisTrait.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public function getAllKeys(): array {
360360
$filter = Http::get('s', '*');
361361
$this->template->addGlobal('search_value', $filter);
362362

363-
if (isset($this->servers[$this->current_server]['scansize']) || !$this->isCommandSupported('KEYS')) {
363+
if (isset($this->servers[$this->current_server]['scansize']) || !$this->redis->isCommandSupported('KEYS')) {
364364
$scansize = (int) ($this->servers[$this->current_server]['scansize'] ?? 1000);
365365
$keys_array = $this->redis->scanKeys($filter, $scansize);
366366
} else {
@@ -376,18 +376,6 @@ public function getAllKeys(): array {
376376
return $this->keysTableView($keys);
377377
}
378378

379-
private function isCommandSupported(string $command): bool {
380-
try {
381-
$commands = $this->redis->rawCommand('COMMAND');
382-
$command_names = array_column($commands, 0);
383-
$is_supported = in_array(strtolower($command), $command_names, true);
384-
} catch (Exception) {
385-
$is_supported = false;
386-
}
387-
388-
return $is_supported;
389-
}
390-
391379
/**
392380
* @param array<int|string, mixed> $keys
393381
*

0 commit comments

Comments
 (0)