@@ -57,18 +57,6 @@ public function __construct(Cache $config)
5757 $ this ->config = array_merge ($ this ->config , $ config ->memcached );
5858 }
5959
60- /**
61- * Closes the connection to Memcache(d) if present.
62- */
63- public function __destruct ()
64- {
65- if ($ this ->memcached instanceof Memcached) {
66- $ this ->memcached ->quit ();
67- } elseif ($ this ->memcached instanceof Memcache) {
68- $ this ->memcached ->close ();
69- }
70- }
71-
7260 public function initialize (): void
7361 {
7462 try {
@@ -230,4 +218,46 @@ public function isSupported(): bool
230218 {
231219 return extension_loaded ('memcached ' ) || extension_loaded ('memcache ' );
232220 }
221+
222+ public function ping (): bool
223+ {
224+ $ version = $ this ->memcached ->getVersion ();
225+
226+ if ($ this ->memcached instanceof Memcached) {
227+ // Memcached extension returns array with server:port => version
228+ if (! is_array ($ version )) {
229+ return false ;
230+ }
231+
232+ $ serverKey = $ this ->config ['host ' ] . ': ' . $ this ->config ['port ' ];
233+
234+ return isset ($ version [$ serverKey ]) && $ version [$ serverKey ] !== false ;
235+ }
236+
237+ if ($ this ->memcached instanceof Memcache) {
238+ // Memcache extension returns string version
239+ return is_string ($ version ) && $ version !== '' ;
240+ }
241+
242+ return false ;
243+ }
244+
245+ public function reconnect (): bool
246+ {
247+ if ($ this ->memcached instanceof Memcached) {
248+ $ this ->memcached ->quit ();
249+ } elseif ($ this ->memcached instanceof Memcache) {
250+ $ this ->memcached ->close ();
251+ }
252+
253+ try {
254+ $ this ->initialize ();
255+
256+ return true ;
257+ } catch (CriticalError $ e ) {
258+ log_message ('error ' , 'Cache: Memcached reconnection failed: ' . $ e ->getMessage ());
259+
260+ return false ;
261+ }
262+ }
233263}
0 commit comments