8000 [Cache] Fix lazy Memcached connections · symfony/symfony@d8c400b · GitHub
[go: up one dir, main page]

Skip to content

Commit d8c400b

Browse files
[Cache] Fix lazy Memcached connections
1 parent 9db03c1 commit d8c400b

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/Symfony/Component/Cache/Traits/MemcachedTrait.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ trait MemcachedTrait
3030
);
3131

3232
private $client;
33+
private $lazyClient;
3334

3435
public static function isSupported()
3536
{
@@ -41,14 +42,18 @@ private function init(\Memcached $client, $namespace, $defaultLifetime)
4142
if (!static::isSupported()) {
4243
throw 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

5056
parent::__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');
203208
try {
204-
return $this->checkResultCode($this->client->getMulti($ids));
209+
return $this->checkResultCode($this->getClient()->getMulti($ids));
205210
} catch (\Error $e) {
206211
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
207212
} finally {
@@ -214,7 +219,7 @@ protected function doFetch(array $ids)
214219
*/
215220
protected 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)
223228
protected 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) {
227232
if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) {
228233
$ok = false;
229234
}
@@ -237,7 +242,7 @@ protected function doDelete(array $ids)
237242
*/
238243
protected function doClear($namespace)
239244
{
240-
return $this->checkResultCode($this->client->flush());
245+
return $this->checkResultCode($this->getClient()->flush());
241246
}
242247

243248
private function checkResultCode($result)
@@ -250,4 +255,24 @@ private function checkResultCode($result)
250255

251256
throw 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
}

0 commit comments

Comments
 (0)
0