Skip to content

Commit d049c7d

Browse files
committed
Optimize Redis dashboard and more tests
1 parent 5423ca9 commit d049c7d

File tree

9 files changed

+88
-104
lines changed

9 files changed

+88
-104
lines changed

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,34 @@ public function pipelineKeys(array $keys): array {
143143

144144
foreach ($keys as $i => $key) {
145145
$index = $i * 3;
146-
147146
$data[$key] = [
148-
'ttl' => $results[$index],
149-
'type' => $this->getType((string) $results[$index + 1]),
150-
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
147+
'ttl' => $results[$index],
148+
'type' => (string) $results[$index + 1],
149+
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
150+
'count' => null,
151151
];
152152
}
153153

154+
$type_results = $this->pipeline(function ($pipe) use ($keys, $data): void {
155+
foreach ($keys as $key) {
156+
$lua_script = 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_script, 1, $key);
166+
}
167+
});
168+
169+
foreach ($keys as $i => $key) {
170+
$type = $data[$key]['type'];
171+
$data[$key]['count'] = ($type !== 'none' && isset($type_results[$i]) && is_int($type_results[$i])) ? $type_results[$i] : null;
172+
}
173+
154174
return $data;
155175
}
156176

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,35 @@ public function pipelineKeys(array $keys): array {
155155
$pipeline->ttl($key);
156156
$pipeline->type($key);
157157
$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);
158163
}
159164

160165
$results = $pipeline->exec();
161166

162167
$data = [];
163168

164169
foreach ($keys as $i => $key) {
165-
$index = $i * 3;
170+
$index = $i * 8;
171+
$type = $this->getType($results[$index + 1]);
172+
173+
$count = match ($type) {
174+
'set' => is_int($results[$index + 3]) ? $results[$index + 3] : null,
175+
'list' => is_int($results[$index + 4]) ? $results[$index + 4] : null,
176+
'zset' => is_int($results[$index + 5]) ? $results[$index + 5] : null,
177+
'hash' => is_int($results[$index + 6]) ? $results[$index + 6] : null,
178+
'stream' => is_int($results[$index + 7]) ? $results[$index + 7] : null,
179+
default => null,
180+
};
166181

167182
$data[$key] = [
168-
'ttl' => $results[$index],
169-
'type' => $this->getType($results[$index + 1]),
170-
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
183+
'ttl' => $results[$index],
184+
'type' => $type,
185+
'size' => is_int($results[$index + 2]) ? $results[$index + 2] : 0,
186+
'count' => $count,
171187
];
172188
}
173189

src/Dashboards/Redis/RedisDashboard.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class RedisDashboard implements DashboardInterface {
2929

3030
public Compatibility\Redis|Compatibility\Predis $redis;
3131

32-
public function __construct(private readonly Template $template) {
32+
public string $clinet = '';
33+
34+
public function __construct(private readonly Template $template, ?string $clinet = null) {
35+
$this->clinet = $clinet ?? (extension_loaded('redis') ? 'redis' : 'predis');
3336
$this->servers = Config::get('redis', []);
3437

3538
$server = Http::get('server', 0);
@@ -78,10 +81,10 @@ public function connect(array $server): Compatibility\Redis|Compatibility\Predis
7881
$server['password'] = trim(file_get_contents($server['authfile']));
7982
}
8083

81-
if (extension_loaded('redis')) {
84+
if ($this->clinet === 'redis') {
8285
$redis = new Compatibility\Redis();
8386
$redis->connection($server);
84-
} elseif (class_exists(Predis::class)) {
87+
} elseif ($this->clinet === 'predis') {
8588
$redis = new Compatibility\Predis($server);
8689
} else {
8790
throw new DashboardException('Redis extension or Predis is not installed.');

src/Dashboards/Redis/RedisTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ trait RedisTrait {
2323
use RedisTypes;
2424

2525
private function panels(): string {
26-
if (extension_loaded('redis')) {
26+
if ($this->clinet === 'redis') {
2727
$title = 'PHP Redis extension v'.phpversion('redis');
28-
} elseif (class_exists(Predis::class)) {
28+
} elseif ($this->clinet === 'predis') {
2929
$title = 'Predis v'.Predis::VERSION;
3030
}
3131

@@ -246,9 +246,9 @@ public function saveKey(): void {
246246
$value = Value::converter(Http::post('value', ''), Http::post('encoder', ''), 'save');
247247
$old_value = Http::post('old_value', '');
248248
$type = Http::post('redis_type', '');
249-
$old_key = (string) Http::post('old_key', '');
249+
$old_key = Http::post('old_key', '');
250250

251-
if ($old_key !== $key) {
251+
if ($old_key !== '' && $old_key !== $key) { // @phpstan-ignore-line
252252
$this->redis->rename($old_key, $key);
253253
}
254254

@@ -344,7 +344,7 @@ private function getAllKeys(): array {
344344
foreach ($keys_array as $key) {
345345
$ttl = $pipeline[$key]['ttl'];
346346
$type = $pipeline[$key]['type'];
347-
$total = $this->getCountOfItemsInKey($type, $key);
347+
$total = $pipeline[$key]['count'];
348348

349349
$keys[] = [
350350
'key' => $key,

src/Dashboards/Redis/RedisTypes.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,4 @@ public function deleteSubKey(string $type, string $key, int|string|null $subkey
217217
default:
218218
}
219219
}
220-
221-
/**
222-
* @throws Exception
223-
*/
224-
private function getCountOfItemsInKey(string $type, string $key): ?int {
225-
return match ($type) {
226-
'set' => $this->redis->sCard($key),
227-
'list' => $this->redis->lLen($key),
228-
'zset' => $this->redis->zCard($key),
229-
'hash' => $this->redis->hLen($key),
230-
'stream' => $this->redis->xLen($key),
231-
default => null,
232-
};
233-
}
234220
}

tests/Clients/PredisTest.php

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* This file is part of the phpCacheAdmin.
4+
* Copyright (c) Róbert Kelčák (https://kelcak.com/)
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Dashboards\Redis;
10+
11+
use Tests\Dashboards\Redis\RedisTestCase;
12+
13+
class PredisTest extends RedisTestCase {
14+
protected string $client = 'predis';
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* This file is part of the phpCacheAdmin.
4+
* Copyright (c) Róbert Kelčák (https://kelcak.com/)
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Tests\Dashboards\Redis;
10+
11+
class RedisTest extends RedisTestCase {
12+
protected string $client = 'redis';
13+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
declare(strict_types=1);
88

9-
namespace Tests\Dashboards;
9+
namespace Tests\Dashboards\Redis;
1010

1111
use Exception;
1212
use PHPUnit\Framework\Attributes\DataProvider;
@@ -20,19 +20,21 @@
2020
use RobiNN\Pca\Template;
2121
use Tests\TestCase;
2222

23-
final class RedisTest extends TestCase {
23+
abstract class RedisTestCase extends TestCase {
2424
private Template $template;
2525

2626
private RedisDashboard $dashboard;
2727

2828
private Predis|Redis $redis;
2929

30+
protected string $client;
31+
3032
/**
3133
* @throws DashboardException
3234
*/
3335
protected function setUp(): void {
3436
$this->template = new Template();
35-
$this->dashboard = new RedisDashboard($this->template);
37+
$this->dashboard = new RedisDashboard($this->template, $this->client);
3638
$this->redis = $this->dashboard->connect([
3739
'host' => Config::get('redis')[0]['host'],
3840
'port' => Config::get('redis')[0]['port'],

0 commit comments

Comments
 (0)