Skip to content

Commit 2ef6ec9

Browse files
committed
Update Memcached classes
1 parent 218fe24 commit 2ef6ec9

7 files changed

Lines changed: 128 additions & 94 deletions

File tree

src/Dashboards/Memcached/MemcacheCompatibility/RunCommandTrait.php renamed to src/Dashboards/Memcached/Compatibility/CommandTrait.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,32 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility;
13+
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

15-
trait RunCommandTrait {
15+
use RobiNN\Pca\Dashboards\DashboardException;
16+
17+
trait CommandTrait {
1618
/**
1719
* Run command.
1820
*
19-
* https://github.com/memcached/memcached/wiki/Commands
20-
*
21-
* @param string $command
21+
* @param string $command https://github.com/memcached/memcached/wiki/Commands
2222
*
2323
* @return ?array<int, mixed>
24+
* @throws DashboardException
2425
*/
25-
public function runCommand(string $command): ?array {
26+
public function command(string $command): ?array {
2627
$data = [];
2728

2829
if (isset($this->server['path'])) {
29-
$fp = stream_socket_client('unix://'.$this->server['path']);
30+
$fp = @stream_socket_client('unix://'.$this->server['path'], $error_code, $error_message);
3031
} else {
3132
$this->server['port'] ??= 11211;
3233

33-
$fp = fsockopen($this->server['host'], (int) $this->server['port'], $error_code, $error_message, 3);
34+
$fp = @fsockopen($this->server['host'], (int) $this->server['port'], $error_code, $error_message, 3);
35+
}
36+
37+
if ($error_message !== '') {
38+
throw new DashboardException('command() method: '.$error_message);
3439
}
3540

3641
if ($fp === false) {
@@ -63,7 +68,7 @@ public function runCommand(string $command): ?array {
6368
}
6469

6570
/**
66-
* Get data from line.
71+
* Format key output.
6772
*
6873
* @param string $line
6974
*
@@ -92,12 +97,16 @@ private function keyData(string $line): array {
9297
/**
9398
* Get all keys.
9499
*
100+
* This command requires Memcached server >= 1.4.18
101+
* https://github.com/memcached/memcached/wiki/ReleaseNotes1418#lru-crawler
102+
*
95103
* @return array<int, mixed>
104+
* @throws DashboardException
96105
*/
97106
public function getKeys(): array {
98107
static $keys = [];
99108

100-
$all_keys = $this->runCommand('lru_crawler metadump all');
109+
$all_keys = $this->command('lru_crawler metadump all');
101110

102111
if ($all_keys !== null) {
103112
foreach ($all_keys as $line) {
@@ -114,9 +123,10 @@ public function getKeys(): array {
114123
* @param string $key
115124
*
116125
* @return string|false
126+
* @throws DashboardException
117127
*/
118128
public function getKey(string $key) {
119-
$data = $this->runCommand('get '.$key);
129+
$data = $this->command('get '.$key);
120130

121131
if (!isset($data[0])) {
122132
return false;
@@ -131,6 +141,7 @@ public function getKey(string $key) {
131141
* @param string $key
132142
*
133143
* @return bool
144+
* @throws DashboardException
134145
*/
135146
public function exists(string $key): bool {
136147
return $this->getKey($key) !== false;

src/Dashboards/Memcached/MemcacheCompatibility/MemcacheInterface.php renamed to src/Dashboards/Memcached/Compatibility/CompatibilityInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility;
13+
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

15-
interface MemcacheInterface {
15+
interface CompatibilityInterface {
1616
/**
1717
* Check connection.
1818
*

src/Dashboards/Memcached/MemcacheCompatibility/Memcache.php renamed to src/Dashboards/Memcached/Compatibility/Memcache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility;
13+
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

15-
class Memcache extends \Memcache implements MemcacheInterface {
16-
use RunCommandTrait;
15+
class Memcache extends \Memcache implements CompatibilityInterface {
16+
use CommandTrait;
1717

1818
/**
1919
* @var array<string, int|string>

src/Dashboards/Memcached/MemcacheCompatibility/Memcached.php renamed to src/Dashboards/Memcached/Compatibility/Memcached.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility;
13+
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

15-
class Memcached extends \Memcached implements MemcacheInterface {
16-
use RunCommandTrait;
15+
class Memcached extends \Memcached implements CompatibilityInterface {
16+
use CommandTrait;
1717

1818
/**
1919
* @var array<string, int|string>

src/Dashboards/Memcached/MemcachedDashboard.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,35 @@ public function getDashboardInfo(): array {
6060
*
6161
* @param array<string, int|string> $server
6262
*
63-
* @return MemcacheCompatibility\Memcache|MemcacheCompatibility\Memcached
63+
* @return Compatibility\Memcache|Compatibility\Memcached
6464
* @throws DashboardException
6565
*/
6666
private function connect(array $server) {
6767
if (extension_loaded('memcached')) {
68-
$memcache = new MemcacheCompatibility\Memcached($server);
68+
$memcached = new Compatibility\Memcached($server);
6969
} elseif (extension_loaded('memcache')) {
70-
$memcache = new MemcacheCompatibility\Memcache($server);
70+
$memcached = new Compatibility\Memcache($server);
7171
} else {
7272
throw new DashboardException('Memcache(d) extension is not installed.');
7373
}
7474

7575
if (isset($server['path'])) {
7676
$memcached_server = $server['path'];
7777

78-
$memcache->addServer($server['path'], 0);
78+
$memcached->addServer($server['path'], 0);
7979
} else {
8080
$server['port'] ??= 11211;
8181

8282
$memcached_server = $server['host'].':'.$server['port'];
8383

84-
$memcache->addServer($server['host'], (int) $server['port']);
84+
$memcached->addServer($server['host'], (int) $server['port']);
8585
}
8686

87-
if (!$memcache->isConnected()) {
88-
throw new DashboardException(sprintf('Failed to connect to Memcache(d) server (%s).', $memcached_server));
87+
if (!$memcached->isConnected()) {
88+
throw new DashboardException(sprintf('Failed to connect to Memcache(d) server %s.', $memcached_server));
8989
}
9090

91-
return $memcache;
91+
return $memcached;
9292
}
9393

9494
/**

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
use RobiNN\Pca\Config;
1616
use RobiNN\Pca\Dashboards\DashboardException;
17-
use RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility\Memcache;
18-
use RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility\Memcached;
17+
use RobiNN\Pca\Dashboards\Memcached\Compatibility\Memcache;
18+
use RobiNN\Pca\Dashboards\Memcached\Compatibility\Memcached;
1919
use RobiNN\Pca\Format;
2020
use RobiNN\Pca\Helpers;
2121
use RobiNN\Pca\Http;
@@ -74,6 +74,7 @@ private function deleteAllKeys($memcached): string {
7474
* @param Memcache|Memcached $memcached
7575
*
7676
* @return string
77+
* @throws DashboardException
7778
*/
7879
private function deleteKey($memcached): string {
7980
$keys = explode(',', Http::get('delete'));
@@ -125,6 +126,7 @@ private function moreInfo(array $servers): string {
125126
* @param Memcache|Memcached $memcached
126127
*
127128
* @return array<int, array<string, string|int>>
129+
* @throws DashboardException
128130
*/
129131
private function getAllKeys($memcached): array {
130132
static $keys = [];
@@ -146,6 +148,7 @@ private function getAllKeys($memcached): array {
146148
* @param Memcache|Memcached $memcached
147149
*
148150
* @return string
151+
* @throws DashboardException
149152
*/
150153
private function mainDashboard($memcached): string {
151154
$keys = $this->getAllKeys($memcached);
@@ -171,6 +174,7 @@ private function mainDashboard($memcached): string {
171174
* @param Memcache|Memcached $memcached
172175
*
173176
* @return string
177+
* @throws DashboardException
174178
*/
175179
private function viewKey($memcached): string {
176180
$key = Http::get('key');
@@ -218,6 +222,7 @@ private function viewKey($memcached): string {
218222
* @param Memcache|Memcached $memcached
219223
*
220224
* @return void
225+
* @throws DashboardException
221226
*/
222227
private function import($memcached): void {
223228
if ($_FILES['import']['type'] === 'text/plain') {
@@ -239,6 +244,7 @@ private function import($memcached): void {
239244
* @param Memcache|Memcached $memcached
240245
*
241246
* @return string
247+
* @throws DashboardException
242248
*/
243249
private function form($memcached): string {
244250
$key = Http::get('key');

0 commit comments

Comments
 (0)