@@ -115,36 +115,53 @@ public function __destruct()
115115 */
116116 public function initialize ()
117117 {
118- if (class_exists ('\Memcached ' ))
118+
119+ // Try to connect to Memcache or Memcached, if an issue occurs throw a CriticalError exception,
120+ // so that the CacheFactory can attempt to initiate the next cache handler.
121+ try
119122 {
120- $ this ->memcached = new \Memcached ();
121- if ($ this ->config ['raw ' ])
123+ if (class_exists ('\Memcached ' ))
124+ {
125+ $ this ->memcached = new \Memcached ();
126+ if ($ this ->config ['raw ' ])
127+ {
128+ $ this ->memcached ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
129+ }
130+ }
131+ elseif (class_exists ('\Memcache ' ))
132+ {
133+ $ this ->memcached = new \Memcache ();
134+ }
135+ else
136+ {
137+ throw new CriticalError ('Cache: Not support Memcache(d) extension. ' );
138+ }
139+
140+ if ($ this ->memcached instanceof \Memcached)
122141 {
123- $ this ->memcached ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
142+ $ this ->memcached ->addServer (
143+ $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['weight ' ]
144+ );
145+ }
146+ elseif ($ this ->memcached instanceof \Memcache)
147+ {
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+ );
124152 }
125153 }
126- elseif ( class_exists ( ' \Memcache ' ) )
154+ catch ( CriticalError $ e )
127155 {
128- $ this ->memcached = new \Memcache ();
156+ // If a CriticalError exception occurs, throw it up.
157+ throw $ e ;
129158 }
130- else
159+ catch ( \ Exception $ e )
131160 {
132- throw new CriticalError ('Cache: Not support Memcache(d) extension. ' );
161+ // If an \Exception occurs, convert it into a CriticalError exception and throw it.
162+ throw new CriticalError ('Cache: Memcache(d) connection refused ( ' . $ e ->getMessage () . ') ' );
133163 }
134164
135- if ($ this ->memcached instanceof \Memcached)
136- {
137- $ this ->memcached ->addServer (
138- $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['weight ' ]
139- );
140- }
141- elseif ($ this ->memcached instanceof \Memcache)
142- {
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- );
147- }
148165 }
149166
150167 //--------------------------------------------------------------------
0 commit comments