99namespace RobiNN \Pca \Dashboards \Memcached ;
1010
1111class PHPMem {
12- public const VERSION = '1.1 .0 ' ;
12+ public const VERSION = '2.0 .0 ' ;
1313
1414 /**
1515 * Unknown or incorrect commands can cause an infinite loop,
@@ -23,25 +23,6 @@ class PHPMem {
2323 'lru ' , 'slabs ' , 'me ' , 'mg ' , 'ms ' , 'md ' , 'ma ' , 'cache_memlimit ' , 'verbosity ' , 'quit ' ,
2424 ];
2525
26- /**
27- * Commands without a specific end string.
28- *
29- * @var array<int, string>
30- */
31- private array $ no_end = [
32- 'incr ' , 'decr ' , 'version ' , 'me ' , 'mg ' , 'ms ' , 'md ' , 'ma ' , 'cache_memlimit ' , 'quit ' ,
33- ];
34-
35- /**
36- * Loop until the server returns one of these end strings.
37- *
38- * @var array<int, string>
39- */
40- private array $ with_end = [
41- 'ERROR ' , 'CLIENT_ERROR ' , 'SERVER_ERROR ' , 'STORED ' , 'NOT_STORED ' , 'EXISTS ' , 'NOT_FOUND ' , 'TOUCHED ' ,
42- 'DELETED ' , 'OK ' , 'END ' , 'BUSY ' , 'BADCLASS ' , 'NOSPARE ' , 'NOTFULL ' , 'UNSAFE ' , 'SAME ' , 'RESET ' , 'EN ' ,
43- ];
44-
4526 /**
4627 * @param array<string, int|string> $server
4728 */
@@ -157,10 +138,10 @@ public function getKeys(): array {
157138 private function parseLine (string $ line ): array {
158139 $ data = [];
159140
160- foreach (explode (' ' , $ line ) as $ part ) {
141+ foreach (\ explode (' ' , $ line ) as $ part ) {
161142 if ($ part !== '' ) {
162- [$ key , $ val ] = explode ('= ' , $ part );
163- $ data [$ key ] = is_numeric ($ val ) ? (int ) $ val : $ val ;
143+ [$ key , $ val ] = \ explode ('= ' , $ part );
144+ $ data [$ key ] = \ is_numeric ($ val ) ? (int ) $ val : $ val ;
164145 }
165146 }
166147
@@ -268,15 +249,24 @@ private function streamConnection(string $command, string $command_name): string
268249 $ buffer = '' ;
269250
270251 while (!feof ($ stream )) {
271- $ line = fgets ($ stream , 256 );
252+ $ line = \ fgets ($ stream , 4096 );
272253
273254 if ($ line === false ) {
274255 break ;
275256 }
276257
277258 $ buffer .= $ line ;
278259
279- if ($ this ->checkCommandEnd ($ command_name , $ buffer )) {
260+ // Commands without a specific end string.
261+ if ($ command_name === 'incr ' || $ command_name === 'decr ' || $ command_name === 'version ' ||
262+ $ command_name === 'me ' || $ command_name === 'mg ' || $ command_name === 'ms ' ||
263+ $ command_name === 'md ' || $ command_name === 'ma ' || $ command_name === 'cache_memlimit ' ||
264+ $ command_name === 'quit ' ) {
265+ break ;
266+ }
267+
268+ // Loop until the server returns one of these end strings.
269+ if ($ this ->checkCommandEnd ($ buffer )) {
280270 break ;
281271 }
282272 }
@@ -286,17 +276,26 @@ private function streamConnection(string $command, string $command_name): string
286276 return $ buffer ;
287277 }
288278
289- private function checkCommandEnd (string $ command , string $ buffer ): bool {
290- if (in_array ($ command , $ this ->no_end , true )) {
291- return true ;
292- }
293-
294- foreach ($ this ->with_end as $ ending ) {
295- if (str_ends_with ($ buffer , $ ending ."\r\n" )) {
296- return true ;
297- }
298- }
299-
300- return false ;
279+ private function checkCommandEnd (string $ buffer ): bool {
280+ return
281+ \str_ends_with ($ buffer , "ERROR \r\n" ) ||
282+ \str_ends_with ($ buffer , "CLIENT_ERROR \r\n" ) ||
283+ \str_ends_with ($ buffer , "SERVER_ERROR \r\n" ) ||
284+ \str_ends_with ($ buffer , "STORED \r\n" ) ||
285+ \str_ends_with ($ buffer , "NOT_STORED \r\n" ) ||
286+ \str_ends_with ($ buffer , "EXISTS \r\n" ) ||
287+ \str_ends_with ($ buffer , "NOT_FOUND \r\n" ) ||
288+ \str_ends_with ($ buffer , "TOUCHED \r\n" ) ||
289+ \str_ends_with ($ buffer , "DELETED \r\n" ) ||
290+ \str_ends_with ($ buffer , "OK \r\n" ) ||
291+ \str_ends_with ($ buffer , "END \r\n" ) ||
292+ \str_ends_with ($ buffer , "BUSY \r\n" ) ||
293+ \str_ends_with ($ buffer , "BADCLASS \r\n" ) ||
294+ \str_ends_with ($ buffer , "NOSPARE \r\n" ) ||
295+ \str_ends_with ($ buffer , "NOTFULL \r\n" ) ||
296+ \str_ends_with ($ buffer , "UNSAFE \r\n" ) ||
297+ \str_ends_with ($ buffer , "SAME \r\n" ) ||
298+ \str_ends_with ($ buffer , "RESET \r\n" ) ||
299+ \str_ends_with ($ buffer , "EN \r\n" );
301300 }
302301}
0 commit comments