@@ -115,35 +115,68 @@ public function __destruct()
115115 */
116116 public function initialize ()
117117 {
118- if (class_exists ('\Memcached ' ))
118+ // Try to connect to Memcache or Memcached, if an issue occurs throw a CriticalError exception,
119+ // so that the CacheFactory can attempt to initiate the next cache handler.
120+ try
119121 {
120- $ this ->memcached = new \Memcached ();
121- if ($ this ->config ['raw ' ])
122+ if (class_exists ('\Memcached ' ))
122123 {
123- $ this ->memcached ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
124+ // Create new instance of \Memcached
125+ $ this ->memcached = new \Memcached ();
126+ if ($ this ->config ['raw ' ])
127+ {
128+ $ this ->memcached ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
129+ }
130+
131+ // Add server
132+ $ this ->memcached ->addServer (
133+ $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['weight ' ]
134+ );
135+
136+ // attempt to get status of servers
137+ $ stats = $ this ->memcached ->getStats ();
138+
139+ // $stats should be an associate array with a key in the format of host:port.
140+ // If it doesn't have the key, we know the server is not working as expected.
141+ if ( !isset ($ stats [$ this ->config ['host ' ]. ': ' .$ this ->config ['port ' ]]) )
142+ {
143+ throw new CriticalError ('Cache: Memcached connection failed. ' );
144+ }
124145 }
125- }
126- elseif (class_exists ('\Memcache ' ))
127- {
128- $ this ->memcached = new \Memcache ();
129- }
130- else
131- {
132- throw new CriticalError ('Cache: Not support Memcache(d) extension. ' );
133- }
146+ elseif (class_exists ('\Memcache ' ))
147+ {
148+ // Create new instance of \Memcache
149+ $ this ->memcached = new \Memcache ();
134150
135- if ($ this ->memcached instanceof \Memcached)
151+ // Check if we can connect to the server
152+ $ can_connect = $ this ->memcached ->connect (
153+ $ this ->config ['host ' ], $ this ->config ['port ' ]
154+ );
155+
156+ // If we can't connect, throw a CriticalError exception
157+ if ($ can_connect == false ){
158+ throw new CriticalError ('Cache: Memcache connection failed. ' );
159+ }
160+
161+ // Add server, third parameter is persistence and defaults to TRUE.
162+ $ this ->memcached ->addServer (
163+ $ this ->config ['host ' ], $ this ->config ['port ' ], true , $ this ->config ['weight ' ]
164+ );
165+ }
166+ else
167+ {
168+ throw new CriticalError ('Cache: Not support Memcache(d) extension. ' );
169+ }
170+ }
171+ catch (CriticalError $ e )
136172 {
137- $ this ->memcached ->addServer (
138- $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['weight ' ]
139- );
173+ // If a CriticalError exception occurs, throw it up.
174+ throw $ e ;
140175 }
141- elseif ( $ this -> memcached instanceof \Memcache )
176+ catch ( \ Exception $ e )
142177 {
143- // Third parameter is persistence and defaults to TRUE.
144- $ this ->memcached ->addServer (
145- $ this ->config ['host ' ], $ this ->config ['port ' ], true , $ this ->config ['weight ' ]
146- );
178+ // If an \Exception occurs, convert it into a CriticalError exception and throw it.
179+ throw new CriticalError ('Cache: Memcache(d) connection refused ( ' . $ e ->getMessage () . '). ' );
147180 }
148181 }
149182
0 commit comments