@@ -30,6 +30,7 @@ trait MemcachedTrait
3030 );
3131
3232private $ client ;
33+ private $ lazyClient ;
3334
3435public static function isSupported ()
3536 {
@@ -41,14 +42,18 @@ private function init(\Memcached $client, $namespace, $defaultLifetime)
4142if (!static ::isSupported ()) {
4243throw new CacheException ('Memcached >= 2.2.0 is required ' );
4344 }
44- $ opt =$ client ->getOption (\Memcached::OPT_SERIALIZER );
45- if (\Memcached::SERIALIZER_PHP !==$ opt && \Memcached::SERIALIZER_IGBINARY !==$ opt ) {
46- throw new CacheException ('MemcachedAdapter: "serializer" option must be "php" or "igbinary". ' );
45+ if (get_class ($ client ) ==='Memcached ' ) {
46+ $ opt =$ client ->getOption (\Memcached::OPT_SERIALIZER );
47+ if (\Memcached::SERIALIZER_PHP !==$ opt && \Memcached::SERIALIZER_IGBINARY !==$ opt ) {
48+ throw new CacheException ('MemcachedAdapter: "serializer" option must be "php" or "igbinary". ' );
49+ }
50+ $ this ->maxIdLength -=strlen ($ client ->getOption (\Memcached::OPT_PREFIX_KEY ));
51+ $ this ->client =$ client ;
52+ }else {
53+ $ this ->lazyClient =$ client ;
4754 }
48- $ this ->maxIdLength -=strlen ($ client ->getOption (\Memcached::OPT_PREFIX_KEY ));
4955
5056parent ::__construct ($ namespace ,$ defaultLifetime );
51- $ this ->client =$ client ;
5257 }
5358
5459/**
@@ -191,7 +196,7 @@ protected function doSave(array $values, $lifetime)
191196$ lifetime +=time ();
192197 }
193198
194- return $ this ->checkResultCode ($ this ->client ->setMulti ($ values ,$ lifetime ));
199+ return $ this ->checkResultCode ($ this ->getClient () ->setMulti ($ values ,$ lifetime ));
195200 }
196201
197202/**
@@ -201,7 +206,7 @@ protected function doFetch(array $ids)
201206 {
202207$ unserializeCallbackHandler =ini_set ('unserialize_callback_func ' ,__CLASS__ .'::handleUnserializeCallback ' );
203208try {
204- return $ this ->checkResultCode ($ this ->client ->getMulti ($ ids ));
209+ return $ this ->checkResultCode ($ this ->getClient () ->getMulti ($ ids ));
205210 }catch (\Error $ e ) {
206211throw new \ErrorException ($ e ->getMessage (),$ e ->getCode (),E_ERROR ,$ e ->getFile (),$ e ->getLine ());
207212 }finally {
@@ -214,7 +219,7 @@ protected function doFetch(array $ids)
214219 */
215220protected function doHave ($ id )
216221 {
217- return false !==$ this ->client ->get ($ id ) ||$ this ->checkResultCode (\Memcached::RES_SUCCESS ===$ this ->client ->getResultCode ());
222+ return false !==$ this ->getClient () ->get ($ id ) ||$ this ->checkResultCode (\Memcached::RES_SUCCESS ===$ this ->client ->getResultCode ());
218223 }
219224
220225/**
@@ -223,7 +228,7 @@ protected function doHave($id)
223228protected function doDelete (array $ ids )
224229 {
225230$ ok =true ;
226- foreach ($ this ->checkResultCode ($ this ->client ->deleteMulti ($ ids ))as $ result ) {
231+ foreach ($ this ->checkResultCode ($ this ->getClient () ->deleteMulti ($ ids ))as $ result ) {
227232if (\Memcached::RES_SUCCESS !==$ result && \Memcached::RES_NOTFOUND !==$ result ) {
228233$ ok =false ;
229234 }
@@ -237,7 +242,7 @@ protected function doDelete(array $ids)
237242 */
238243protected function doClear ($ namespace )
239244 {
240- return $ this ->checkResultCode ($ this ->client ->flush ());
245+ return $ this ->checkResultCode ($ this ->getClient () ->flush ());
241246 }
242247
243248private function checkResultCode ($ result )
@@ -250,4 +255,24 @@ private function checkResultCode($result)
250255
251256throw new CacheException (sprintf ('MemcachedAdapter client error: %s. ' ,strtolower ($ this ->client ->getResultMessage ())));
252257 }
258+
259+ /**
260+ * @return \Memcached
261+ */
262+ private function getClient ()
263+ {
264+ if ($ this ->client ) {
265+ return $ this ->client ;
266+ }
267+
268+ $ opt =$ this ->lazyClient ->getOption (\Memcached::OPT_SERIALIZER );
269+ if (\Memcached::SERIALIZER_PHP !==$ opt && \Memcached::SERIALIZER_IGBINARY !==$ opt ) {
270+ throw new CacheException ('MemcachedAdapter: "serializer" option must be "php" or "igbinary". ' );
271+ }
272+ if ('' !==$ prefix = (string )$ this ->lazyClient ->getOption (\Memcached::OPT_PREFIX_KEY )) {
273+ throw new CacheException (sprintf ('MemcachedAdapter: "prefix_key" option must be empty when using proxified connections, "%s" given. ' ,$ prefix ));
274+ }
275+
276+ return $ this ->client =$ this ->lazyClient ;
277+ }
253278}