Skip to content

Commit b1fabc7

Browse files
committed
Update Memcached dashboard
1 parent e5250de commit b1fabc7

7 files changed

Lines changed: 59 additions & 45 deletions

File tree

src/Dashboards/Memcached/Compatibility/CompatibilityInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function isConnected(): bool;
2828
public function getServerStats(): array;
2929

3030
/**
31-
* Store item.
31+
* Alias to a set() but with the same order of parameters.
3232
*
3333
* @param string $key
3434
* @param string $value

src/Dashboards/Memcached/Compatibility/CommandTrait.php renamed to src/Dashboards/Memcached/Compatibility/KeysTrait.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
use RobiNN\Pca\Dashboards\Memcached\MemcachedException;
1616

17-
trait CommandTrait {
17+
trait KeysTrait {
1818
/**
1919
* Run command.
2020
*
21-
* @param string $command https://github.com/memcached/memcached/wiki/Commands
22-
*
23-
* @return ?array<int, mixed>
21+
* @return array<int, mixed>
2422
* @throws MemcachedException
2523
*/
26-
public function command(string $command): ?array {
24+
public function command(string $command): array {
2725
if (isset($this->server['path'])) {
2826
$fp = @stream_socket_client('unix://'.$this->server['path'], $error_code, $error_message);
2927
} else {
@@ -32,23 +30,19 @@ public function command(string $command): ?array {
3230
$fp = @fsockopen($this->server['host'], (int) $this->server['port'], $error_code, $error_message, 3);
3331
}
3432

35-
if ($error_message !== '') {
36-
throw new MemcachedException('command() method: '.$error_message);
37-
}
38-
39-
if ($fp === false) {
40-
return null;
33+
if ($error_message !== '' || $fp === false) {
34+
throw new MemcachedException('Command: "'.$command.'": '.$error_message);
4135
}
4236

4337
fwrite($fp, $command."\r\n");
4438

45-
$part = '';
39+
$buffer = '';
4640
$data = [];
4741

4842
while (!feof($fp)) {
49-
$part .= fgets($fp, 1024);
50-
$lines = explode("\n", $part);
51-
$part = array_pop($lines);
43+
$buffer .= fgets($fp, 1024);
44+
$lines = explode("\n", $buffer);
45+
$buffer = array_pop($lines);
5246

5347
foreach ($lines as $line) {
5448
$line = trim($line);

src/Dashboards/Memcached/Compatibility/Memcache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use RobiNN\Pca\Dashboards\Memcached\MemcachedException;
1616

1717
class Memcache extends \Memcache implements CompatibilityInterface {
18-
use CommandTrait;
18+
use KeysTrait;
1919

2020
/**
2121
* @var array<string, int|string>
@@ -50,7 +50,7 @@ public function getServerStats(): array {
5050
}
5151

5252
/**
53-
* Store item.
53+
* Alias to a set() but with the same order of parameters.
5454
*
5555
* @param string $key
5656
* @param string $value

src/Dashboards/Memcached/Compatibility/Memcached.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

1515
class Memcached extends \Memcached implements CompatibilityInterface {
16-
use CommandTrait;
16+
use KeysTrait;
1717

1818
/**
1919
* @var array<string, int|string>
@@ -48,7 +48,7 @@ public function getServerStats(): array {
4848
}
4949

5050
/**
51-
* Store item.
51+
* Alias to a set() but with the same order of parameters.
5252
*
5353
* @param string $key
5454
* @param string $value

src/Dashboards/Memcached/Compatibility/PHPMem.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use RobiNN\Pca\Helpers;
1717

1818
class PHPMem implements CompatibilityInterface {
19-
use CommandTrait;
19+
use KeysTrait;
2020

2121
/**
2222
* @const string PHPMem version.
@@ -59,17 +59,18 @@ public function addServer(string $host, int $port = 11211): bool {
5959
*
6060
* @param string $command
6161
*
62-
* @return ?string
62+
* @return string
63+
* @throws MemcachedException
6364
*/
64-
private function send(string $command): ?string {
65+
private function send(string $command): string {
6566
if (isset($this->server['path'])) {
66-
$fp = @stream_socket_client('unix://'.$this->host);
67+
$fp = @stream_socket_client('unix://'.$this->host, $error_code, $error_message);
6768
} else {
6869
$fp = @fsockopen($this->host, $this->port, $error_code, $error_message, 3);
6970
}
7071

71-
if ($fp === false) {
72-
return null;
72+
if ($error_message !== '' || $fp === false) {
73+
throw new MemcachedException('Command: "'.$command.'": '.$error_message);
7374
}
7475

7576
fwrite($fp, $command."\r\n");
@@ -99,15 +100,16 @@ private function send(string $command): ?string {
99100
* @param int $expiration
100101
*
101102
* @return bool
103+
* @throws MemcachedException
102104
*/
103105
public function set(string $key, $value, int $expiration = 0): bool {
104106
$type = gettype($value);
105107

106-
if (($type !== 'string' && $type !== 'integer' && $type !== 'double' && $type !== 'boolean') === true) {
108+
if ($type !== 'string' && $type !== 'integer' && $type !== 'double' && $type !== 'boolean') {
107109
$value = serialize($value);
108110
}
109111

110-
$raw = $this->send('set'.' '.$key.' '.'0 '.$expiration.' '.strlen($value)."\r\n".$value."\r\n");
112+
$raw = $this->send('set'.' '.$key.' '.'0 '.$expiration.' '.strlen((string) $value)."\r\n".$value."\r\n");
111113

112114
if (Helpers::str_starts_with($raw, 'STORED')) {
113115
return true;
@@ -122,6 +124,7 @@ public function set(string $key, $value, int $expiration = 0): bool {
122124
* @param string $key
123125
*
124126
* @return string|false
127+
* @throws MemcachedException
125128
*/
126129
public function get(string $key) {
127130
$raw = $this->send('get '.$key);
@@ -140,6 +143,7 @@ public function get(string $key) {
140143
* @param string $key
141144
*
142145
* @return bool
146+
* @throws MemcachedException
143147
*/
144148
public function delete(string $key): bool {
145149
$raw = $this->send('delete '.$key);
@@ -151,6 +155,7 @@ public function delete(string $key): bool {
151155
* Invalidate all items in the cache.
152156
*
153157
* @return bool
158+
* @throws MemcachedException
154159
*/
155160
public function flush(): bool {
156161
$raw = $this->send('flush_all');
@@ -162,6 +167,7 @@ public function flush(): bool {
162167
* Check connection.
163168
*
164169
* @return bool
170+
* @throws MemcachedException
165171
*/
166172
public function isConnected(): bool {
167173
$stats = $this->getServerStats();
@@ -173,6 +179,7 @@ public function isConnected(): bool {
173179
* Get server statistics.
174180
*
175181
* @return array<string, mixed>
182+
* @throws MemcachedException
176183
*/
177184
public function getServerStats(): array {
178185
$raw = $this->send('stats');
@@ -194,13 +201,14 @@ public function getServerStats(): array {
194201
}
195202

196203
/**
197-
* Store item.
204+
* Alias to a set() but with the same order of parameters.
198205
*
199206
* @param string $key
200207
* @param string $value
201208
* @param int $expiration
202209
*
203210
* @return bool
211+
* @throws MemcachedException
204212
*/
205213
public function store(string $key, string $value, int $expiration = 0): bool {
206214
return $this->set($key, $value, $expiration);

src/Dashboards/Memcached/MemcachedDashboard.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class_exists(Compatibility\PHPMem::class);
5353
public function getDashboardInfo(): array {
5454
return [
5555
'key' => 'memcached',
56-
'title' => 'Memcache(d)',
56+
'title' => 'Memcached',
5757
'color' => 'emerald',
5858
];
5959
}
@@ -63,7 +63,7 @@ public function getDashboardInfo(): array {
6363
*
6464
* @param array<string, int|string> $server
6565
*
66-
* @return Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem
66+
* @return Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem
6767
* @throws DashboardException
6868
*/
6969
private function connect(array $server) {
@@ -100,8 +100,12 @@ private function connect(array $server) {
100100
}
101101
}
102102

103-
if (!$memcached->isConnected()) {
104-
throw new DashboardException(sprintf('Failed to connect to Memcached server %s.', $memcached_server));
103+
try {
104+
if (!$memcached->isConnected()) {
105+
throw new DashboardException(sprintf('Failed to connect to Memcached server %s.', $memcached_server));
106+
}
107+
} catch (MemcachedException $e) {
108+
throw new DashboardException($e->getMessage());
105109
}
106110

107111
return $memcached;

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ private function serverInfo(array $servers): array {
3434
$memcached = $this->connect($servers[Http::get('panel', 'int')]);
3535
$server_info = $memcached->getServerStats();
3636

37+
try {
38+
$keys = Format::number(count($memcached->getKeys()));
39+
} catch (MemcachedException $e) {
40+
$keys = 'An error occurred while retrieving keys.';
41+
}
42+
3743
return [
3844
'Version' => $server_info['version'],
3945
'Open connections' => $server_info['curr_connections'],
4046
'Uptime' => Format::seconds((int) $server_info['uptime']),
4147
'Cache limit' => Format::bytes((int) $server_info['limit_maxbytes']),
4248
'Used' => Format::bytes((int) $server_info['bytes']),
43-
'Keys' => Format::number(count($memcached->getKeys())),
49+
'Keys' => $keys,
4450
];
4551
} catch (DashboardException|MemcachedException $e) {
4652
return [
@@ -52,9 +58,10 @@ private function serverInfo(array $servers): array {
5258
/**
5359
* Delete all keys.
5460
*
55-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
61+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
5662
*
5763
* @return string
64+
* @throws MemcachedException
5865
*/
5966
private function deleteAllKeys($memcached): string {
6067
if ($memcached->flush()) {
@@ -69,7 +76,7 @@ private function deleteAllKeys($memcached): string {
6976
/**
7077
* Delete key or selected keys.
7178
*
72-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
79+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
7380
*
7481
* @return string
7582
* @throws MemcachedException
@@ -115,15 +122,15 @@ private function moreInfo(array $servers): string {
115122
'panel_title' => $server_data['name'] ?? $server_data['host'].':'.$server_data['port'],
116123
'array' => Helpers::convertBoolToString($info),
117124
]);
118-
} catch (DashboardException $e) {
125+
} catch (DashboardException|MemcachedException $e) {
119126
return $e->getMessage();
120127
}
121128
}
122129

123130
/**
124131
* Get all keys with data.
125132
*
126-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
133+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
127134
*
128135
* @return array<int, array<string, string|int>>
129136
* @throws MemcachedException
@@ -135,7 +142,7 @@ private function getAllKeys($memcached): array {
135142
$keys[] = [
136143
'key' => $key_data['key'],
137144
'ttl' => $key_data['exp'],
138-
'type' => 'string', // In Memcache(d) everything is stored as a string.
145+
'type' => 'string', // In Memcached everything is stored as a string. Calling gettype() will slow down page loading.
139146
];
140147
}
141148

@@ -145,7 +152,7 @@ private function getAllKeys($memcached): array {
145152
/**
146153
* Main dashboard content.
147154
*
148-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
155+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
149156
*
150157
* @return string
151158
* @throws MemcachedException
@@ -171,7 +178,7 @@ private function mainDashboard($memcached): string {
171178
/**
172179
* View key value.
173180
*
174-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
181+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
175182
*
176183
* @return string
177184
* @throws MemcachedException
@@ -205,7 +212,7 @@ private function viewKey($memcached): string {
205212
return $this->template->render('partials/view_key', [
206213
'key' => $key,
207214
'value' => $value,
208-
'type' => 'string', // In Memcache(d) everything is stored as a string.
215+
'type' => null,
209216
'ttl' => Format::seconds($ttl),
210217
'size' => Format::bytes(strlen($value)),
211218
'encode_fn' => $encode_fn,
@@ -219,7 +226,7 @@ private function viewKey($memcached): string {
219226
/**
220227
* Import key.
221228
*
222-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
229+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
223230
*
224231
* @return void
225232
* @throws MemcachedException
@@ -241,7 +248,7 @@ private function import($memcached): void {
241248
/**
242249
* Add/edit form.
243250
*
244-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
251+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
245252
*
246253
* @return string
247254
* @throws MemcachedException
@@ -276,9 +283,10 @@ private function form($memcached): string {
276283
/**
277284
* Save key.
278285
*
279-
* @param Compatibility\Memcache|Compatibility\Memcached|Compatibility\PHPMem $memcached
286+
* @param Compatibility\Memcached|Compatibility\Memcache|Compatibility\PHPMem $memcached
280287
*
281288
* @return void
289+
* @throws MemcachedException
282290
*/
283291
private function saveKey($memcached): void {
284292
$key = Http::post('key');

0 commit comments

Comments
 (0)