@@ -115,40 +115,49 @@ public function __destruct()
115115 */
116116 public function initialize ()
117117 {
118-
119118 // Try to connect to Memcache or Memcached, if an issue occurs throw a CriticalError exception,
120119 // so that the CacheFactory can attempt to initiate the next cache handler.
121120 try
122121 {
123122 if (class_exists ('\Memcached ' ))
124123 {
124+ // Create new instance of \Memcached
125125 $ this ->memcached = new \Memcached ();
126126 if ($ this ->config ['raw ' ])
127127 {
128128 $ this ->memcached ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
129129 }
130+
131+ // TODO: check if we can connect to the server
132+
133+ // Add server
134+ $ this ->memcached ->addServer (
135+ $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['weight ' ]
136+ );
130137 }
131138 elseif (class_exists ('\Memcache ' ))
132139 {
140+ // Create new instance of \Memcache
133141 $ this ->memcached = new \Memcache ();
134- }
135- else
136- {
137- throw new CriticalError ('Cache: Not support Memcache(d) extension. ' );
138- }
139142
140- if ($ this ->memcached instanceof \Memcached)
141- {
143+ // Check if we can connect to the server
144+ $ can_connect = $ this ->memcached ->connect (
145+ $ this ->config ['host ' ], $ this ->config ['port ' ]
146+ );
147+
148+ // If we can't connect, throw a CriticalError exception
149+ if ($ can_connect == false ){
150+ throw new CriticalError ('Cache: Memcache connection failed. ' );
151+ }
152+
153+ // Add server, third parameter is persistence and defaults to TRUE.
142154 $ this ->memcached ->addServer (
143- $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['weight ' ]
155+ $ this ->config ['host ' ], $ this ->config ['port ' ], true , $ this ->config ['weight ' ]
144156 );
145157 }
146- elseif ( $ this -> memcached instanceof \Memcache)
158+ else
147159 {
148- // Third parameter is persistence and defaults to TRUE.
149- $ this ->memcached ->addServer (
150- $ this ->config ['host ' ], $ this ->config ['port ' ], true , $ this ->config ['weight ' ]
151- );
160+ throw new CriticalError ('Cache: Not support Memcache(d) extension. ' );
152161 }
153162 }
154163 catch (CriticalError $ e )
@@ -161,7 +170,6 @@ public function initialize()
161170 // If an \Exception occurs, convert it into a CriticalError exception and throw it.
162171 throw new CriticalError ('Cache: Memcache(d) connection refused ( ' . $ e ->getMessage () . ') ' );
163172 }
164-
165173 }
166174
167175 //--------------------------------------------------------------------
0 commit comments