Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Cache] Fix lazy Memcached connections #23971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions 45 src/Symfony/Component/Cache/Traits/MemcachedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ trait MemcachedTrait
);

private $client;
private $lazyClient;

public static function isSupported()
{
Expand All @@ -41,14 +42,18 @@ private function init(\Memcached $client, $namespace, $defaultLifetime)
if (!static::isSupported()) {
throw new CacheException('Memcached >= 2.2.0 is required');
}
$opt = $client->getOption(\Memcached::OPT_SERIALIZER);
if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
throw new CacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
if (get_class($client) === 'Memcached') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yoda

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is yoda here? :)

$opt = $client->getOption(\Memcached::OPT_SERIALIZER);
if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
throw new CacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
}
$this->maxIdLength -= strlen($client->getOption(\Memcached::OPT_PREFIX_KEY));
$this->client = $client;
} else {
$this->lazyClient = $client;
}
$this->maxIdLength -= strlen($client->getOption(\Memcached::OPT_PREFIX_KEY));

parent::__construct($namespace, $defaultLifetime);
$this->client = $client;
}

/**
Expand Down Expand Up @@ -191,7 +196,7 @@ protected function doSave(array $values, $lifetime)
$lifetime += time();
}

return $this->checkResultCode($this->client->setMulti($values, $lifetime));
return $this->checkResultCode($this->getClient()->setMulti($values, $lifetime));
}

/**
Expand All @@ -201,7 +206,7 @@ protected function doFetch(array $ids)
{
$unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
try {
return $this->checkResultCode($this->client->getMulti($ids));
return $this->checkResultCode($this->getClient()->getMulti($ids));
} catch (\Error $e) {
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
} finally {
Expand All @@ -214,7 +219,7 @@ protected function doFetch(array $ids)
*/
protected function doHave($id)
{
return false !== $this->client->get($id) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
return false !== $this->getClient()->get($id) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
}

/**
Expand All @@ -223,7 +228,7 @@ protected function doHave($id)
protected function doDelete(array $ids)
{
$ok = true;
foreach ($this->checkResultCode($this->client->deleteMulti($ids)) as $result) {
foreach ($this->checkResultCode($this->getClient()->deleteMulti($ids)) as $result) {
if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) {
$ok = false;
}
Expand All @@ -237,7 +242,7 @@ protected function doDelete(array $ids)
*/
protected function doClear($namespace)
{
return $this->checkResultCode($this->client->flush());
return $this->checkResultCode($this->getClient()->flush());
}

private function checkResultCode($result)
Expand All @@ -250,4 +255,24 @@ private function checkResultCode($result)

throw new CacheException(sprintf('MemcachedAdapter client error: %s.', strtolower($this->client->getResultMessage())));
}

/**
* @return \Memcached
*/
private function getClient()
{
if ($this->client) {
return $this->client;
}

$opt = $this->lazyClient->getOption(\Memcached::OPT_SERIALIZER);
if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
throw new CacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
}
if ('' !== $prefix = (string) $this->lazyClient->getOption(\Memcached::OPT_PREFIX_KEY)) {
throw new CacheException(sprintf('MemcachedAdapter: "prefix_key" option must be empty when using proxified connections, "%s" given.', $prefix));
}

return $this->client = $this->lazyClient;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.