88
99namespace RobiNN \Pca \Dashboards \Memcached ;
1010
11+ use Memcached ;
1112use function explode ;
1213use function is_numeric ;
1314use function preg_match ;
@@ -29,6 +30,8 @@ class PHPMem {
2930
3031 private ?string $ server_version = null ;
3132
33+ private ?Memcached $ memcached = null ;
34+
3235 private const NO_END_COMMANDS = [
3336 'me ' => true , 'incr ' => true , 'decr ' => true , 'version ' => true ,
3437 'ms ' => true , 'md ' => true , 'ma ' => true , 'cache_memlimit ' => true ,
@@ -44,9 +47,18 @@ class PHPMem {
4447 ];
4548
4649 /**
47- * @param array<string, int|string> $server
50+ * @param array<string, int|string|bool > $server
4851 */
4952 public function __construct (protected array $ server = []) {
53+ if (isset ($ this ->server ['extension ' ]) && $ this ->server ['extension ' ] === true && extension_loaded ('memcached ' )) {
54+ $ this ->memcached = new Memcached ();
55+
56+ if (isset ($ this ->server ['path ' ])) {
57+ $ this ->memcached ->addServer ($ this ->server ['path ' ], 0 );
58+ } else {
59+ $ this ->memcached ->addServer ($ this ->server ['host ' ], (int ) $ this ->server ['port ' ]);
60+ }
61+ }
5062 }
5163
5264 /**
@@ -55,28 +67,43 @@ public function __construct(protected array $server = []) {
5567 * @throws MemcachedException
5668 */
5769 public function set (string $ key , mixed $ value , int $ expiration = 0 ): bool {
70+ if ($ this ->memcached !== null ) {
71+ return $ this ->memcached ->set ($ key , $ value , $ expiration );
72+ }
73+
5874 $ value = is_scalar ($ value ) ? (string ) $ value : serialize ($ value );
5975 $ raw = $ this ->runCommand ('set ' .$ key .' 0 ' .$ expiration .' ' .strlen ($ value )."\r\n" .$ value );
6076
6177 return str_starts_with ($ raw , 'STORED ' );
6278 }
6379
6480 /**
65- * Retrieve an item .
81+ * Get raw key .
6682 *
6783 * @throws MemcachedException
6884 */
6985 public function get (string $ key ): string |false {
86+ if ($ this ->memcached !== null ) {
87+ $ value = $ this ->memcached ->get ($ key );
88+
89+ if ($ this ->memcached ->getResultCode () === Memcached::RES_NOTFOUND ) {
90+ return false ;
91+ }
92+
93+ return is_scalar ($ value ) ? (string ) $ value : serialize ($ value );
94+ }
95+
7096 $ raw = $ this ->runCommand ('get ' .$ key );
71- $ lines = explode ("\r\n" , $ raw );
97+ $ data = explode ("\r\n" , $ raw );
7298
73- if (str_starts_with ( $ raw , ' VALUE ' ) && str_ends_with ( $ raw , 'END ' ) ) {
74- return $ lines [ 1 ] ;
99+ if ($ data [ 0 ] === 'END ' ) {
100+ return false ;
75101 }
76102
77- return false ;
103+ return ! isset ( $ data [ 1 ]) || $ data [ 1 ] === ' N; ' ? '' : $ data [ 1 ] ;
78104 }
79105
106+
80107 /**
81108 * Delete item from the server.
82109 *
@@ -258,22 +285,6 @@ public function parseLine(string $line): array {
258285 return $ data ;
259286 }
260287
261- /**
262- * Get raw key.
263- *
264- * @throws MemcachedException
265- */
266- public function getKey (string $ key ): string |false {
267- $ raw = $ this ->runCommand ('get ' .$ key );
268- $ data = explode ("\r\n" , $ raw );
269-
270- if ($ data [0 ] === 'END ' ) {
271- return false ;
272- }
273-
274- return !isset ($ data [1 ]) || $ data [1 ] === 'N; ' ? '' : $ data [1 ];
275- }
276-
277288 /**
278289 * Get key meta-data.
279290 *
@@ -315,7 +326,7 @@ public function getKeyMeta(string $key): array {
315326 * @throws MemcachedException
316327 */
317328 public function exists (string $ key ): bool {
318- return $ this ->getKey ($ key ) !== false ;
329+ return $ this ->get ($ key ) !== false ;
319330 }
320331
321332 /**
0 commit comments