1010
1111declare (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 ;
0 commit comments