Skip to content
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

TagAwareAdapter over non-binary memcached connections corrupts memcache #27416

Merged
merged 1 commit into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Simple;

use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Simple\MemcachedCache;

class MemcachedCacheTextModeTest extends MemcachedCacheTest
{
public function createSimpleCache($defaultLifetime = 0)
{
$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false));

return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
}
23 changes: 19 additions & 4 deletions src/Symfony/Component/Cache/Traits/MemcachedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ protected function doSave(array $values, $lifetime)
$lifetime += time();
}

return $this->checkResultCode($this->getClient()->setMulti($values, $lifetime));
$encodedValues = array();
foreach ($values as $key => $value) {
$encodedValues[rawurlencode($key)] = $value;
}

return $this->checkResultCode($this->getClient()->setMulti($encodedValues, $lifetime));
Copy link
Member

Choose a reason for hiding this comment

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

this is not decoding keys in the returned value. Can it have keys in it ?

Copy link
Author

Choose a reason for hiding this comment

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

\Memcached::setMulti returns bool. Did I missed something else?

}

/**
Expand All @@ -208,7 +213,16 @@ protected function doFetch(array $ids)
{
$unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
try {
return $this->checkResultCode($this->getClient()->getMulti($ids));
$encodedIds = array_map('rawurlencode', $ids);

$encodedResult = $this->checkResultCode($this->getClient()->getMulti($encodedIds));

$result = array();
foreach ($encodedResult as $key => $value) {
$result[rawurldecode($key)] = $value;
}

return $result;
} catch (\Error $e) {
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
} finally {
Expand All @@ -221,7 +235,7 @@ protected function doFetch(array $ids)
*/
protected function doHave($id)
{
return false !== $this->getClient()->get($id) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
return false !== $this->getClient()->get(rawurlencode($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
}

/**
Expand All @@ -230,7 +244,8 @@ protected function doHave($id)
protected function doDelete(array $ids)
{
$ok = true;
foreach ($this->checkResultCode($this->getClient()->deleteMulti($ids)) as $result) {
$encodedIds = array_map('rawurlencode', $ids);
foreach ($this->checkResultCode($this->getClient()->deleteMulti($encodedIds)) as $result) {
if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) {
$ok = false;
}
Expand Down