@@ -30,6 +30,7 @@ trait MemcachedTrait
30
30
);
31
31
32
32
private $ client ;
33
+ private $ lazyClient ;
33
34
34
35
public static function isSupported ()
35
36
{
@@ -41,14 +42,18 @@ private function init(\Memcached $client, $namespace, $defaultLifetime)
41
42
if (!static ::isSupported ()) {
42
43
throw new CacheException ('Memcached >= 2.2.0 is required ' );
43
44
}
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 ;
47
54
}
48
- $ this ->maxIdLength -= strlen ($ client ->getOption (\Memcached::OPT_PREFIX_KEY ));
49
55
50
56
parent ::__construct ($ namespace , $ defaultLifetime );
51
- $ this ->client = $ client ;
52
57
}
53
58
54
59
/**
@@ -191,7 +196,7 @@ protected function doSave(array $values, $lifetime)
191
196
$ lifetime += time ();
192
197
}
193
198
194
- return $ this ->checkResultCode ($ this ->client ->setMulti ($ values , $ lifetime ));
199
+ return $ this ->checkResultCode ($ this ->getClient () ->setMulti ($ values , $ lifetime ));
195
200
}
196
201
197
202
/**
@@ -201,7 +206,7 @@ protected function doFetch(array $ids)
201
206
{
202
207
$ unserializeCallbackHandler = ini_set ('unserialize_callback_func ' , __CLASS__ .'::handleUnserializeCallback ' );
203
208
try {
204
- return $ this ->checkResultCode ($ this ->client ->getMulti ($ ids ));
209
+ return $ this ->checkResultCode ($ this ->getClient () ->getMulti ($ ids ));
205
210
} catch (\Error $ e ) {
206
211
throw new \ErrorException ($ e ->getMessage (), $ e ->getCode (), E_ERROR , $ e ->getFile (), $ e ->getLine ());
207
212
} finally {
@@ -214,7 +219,7 @@ protected function doFetch(array $ids)
214
219
*/
215
220
protected function doHave ($ id )
216
221
{
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 ());
218
223
}
219
224
220
225
/**
@@ -223,7 +228,7 @@ protected function doHave($id)
223
228
protected function doDelete (array $ ids )
224
229
{
225
230
$ ok = true ;
226
- foreach ($ this ->checkResultCode ($ this ->client ->deleteMulti ($ ids )) as $ result ) {
231
+ foreach ($ this ->checkResultCode ($ this ->getClient () ->deleteMulti ($ ids )) as $ result ) {
227
232
if (\Memcached::RES_SUCCESS !== $ result && \Memcached::RES_NOTFOUND !== $ result ) {
228
233
$ ok = false ;
229
234
}
@@ -237,7 +242,7 @@ protected function doDelete(array $ids)
237
242
*/
238
243
protected function doClear ($ namespace )
239
244
{
240
- return $ this ->checkResultCode ($ this ->client ->flush ());
245
+ return $ this ->checkResultCode ($ this ->getClient () ->flush ());
241
246
}
242
247
243
248
private function checkResultCode ($ result )
@@ -250,4 +255,24 @@ private function checkResultCode($result)
250
255
251
256
throw new CacheException (sprintf ('MemcachedAdapter client error: %s. ' , strtolower ($ this ->client ->getResultMessage ())));
252
257
}
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
+ }
253
278
}
0 commit comments