Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 380ee83

Browse files
author
Aleksey Prilipko
committed
add key encoding/decoding for doFetch, doHave, doDelete methods
1 parent 5a52c2d commit 380ee83

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,17 @@ public function testNonBinaryMemcachedMode()
204204

205205
$this->assertEquals(1, $simpleCache->getItem('simple')->get(), 'TagAwareAdapter should not corrupt memcached connection in ascii mode');
206206
}
207+
208+
public function testNonAsciiKeysInTextMode()
209+
{
210+
$nonbinaryMemcachedClient = AbstractAdapter::createConnection('memcached://' . getenv('MEMCACHED_HOST'), array('binary_protocol' => false));
211+
$simpleCache = new MemcachedAdapter($nonbinaryMemcachedClient);
212+
213+
$binaryKey = "si\0mp l\ne";
214+
$simpleCache->save($simpleCache->getItem($binaryKey)->set(1));
215+
216+
$this->assertTrue($simpleCache->hasItem($binaryKey));
217+
$this->assertEquals(1, $simpleCache->getItem($binaryKey)->get(), 'MemcachedAdapter should store any key in ascii mode');
218+
$this->assertTrue($simpleCache->deleteItem($binaryKey));
219+
}
207220
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ trait MemcachedTrait
3131

3232
private $client;
3333
private $lazyClient;
34-
private $isTextProtocol;
3534

3635
public static function isSupported()
3736
{
@@ -50,7 +49,6 @@ private function init(\Memcached $client, $namespace, $defaultLifetime)
5049
}
5150
$this->maxIdLength -= strlen($client->getOption(\Memcached::OPT_PREFIX_KEY));
5251
$this->client = $client;
53-
$this->isTextProtocol = !$client->getOption(\Memcached::OPT_BINARY_PROTOCOL);
5452
} else {
5553
$this->lazyClient = $client;
5654
}
@@ -200,13 +198,12 @@ protected function doSave(array $values, $lifetime)
200198
$lifetime += time();
201199
}
202200

203-
if ($this->isTextProtocol) {
204-
$encoded_values = array();
205-
array_walk($values, function ($value, $key) use (&$encoded_values) { $encoded_values[rawurlencode($key)] = $value; });
206-
$values = $encoded_values;
201+
$encodedValues = array();
202+
foreach ($values as $key => $value) {
203+
$encodedValues[rawurlencode($key)] = $value;
207204
}
208205

209-
return $this->checkResultCode($this->getClient()->setMulti($values, $lifetime));
206+
return $this->checkResultCode($this->getClient()->setMulti($encodedValues, $lifetime));
210207
}
211208

212209
/**
@@ -216,11 +213,16 @@ protected function doFetch(array $ids)
216213
{
217214
$unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
218215
try {
219-
if ($this->isTextProtocol) {
220-
$ids = array_map('rawurlencode', $ids);
216+
$encodedIds = array_map('rawurlencode', $ids);
217+
218+
$encodedResult = $this->checkResultCode($this->getClient()->getMulti($encodedIds));
219+
220+
$result = array();
221+
foreach ($encodedResult as $key => $value) {
222+
$result[rawurldecode($key)] = $value;
221223
}
222224

223-
return $this->checkResultCode($this->getClient()->getMulti($ids));
225+
return $result;
224226
} catch (\Error $e) {
225227
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
226228
} finally {
@@ -233,7 +235,7 @@ protected function doFetch(array $ids)
233235
*/
234236
protected function doHave($id)
235237
{
236-
return false !== $this->getClient()->get($id) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
238+
return false !== $this->getClient()->get(rawurlencode($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
237239
}
238240

239241
/**
@@ -242,7 +244,8 @@ protected function doHave($id)
242244
protected function doDelete(array $ids)
243245
{
244246
$ok = true;
245-
foreach ($this->checkResultCode($this->getClient()->deleteMulti($ids)) as $result) {
247+
$encodedIds = array_map('rawurlencode', $ids);
248+
foreach ($this->checkResultCode($this->getClient()->deleteMulti($encodedIds)) as $result) {
246249
if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) {
247250
$ok = false;
248251
}
@@ -287,9 +290,6 @@ private function getClient()
287290
throw new CacheException(sprintf('MemcachedAdapter: "prefix_key" option must be empty when using proxified connections, "%s" given.', $prefix));
288291
}
289292

290-
$this->client = $this->lazyClient;
291-
$this->isTextProtocol = !$this->client->getOption(\Memcached::OPT_BINARY_PROTOCOL);
292-
293-
return $this->client;
293+
return $this->client = $this->lazyClient;
294294
}
295295
}

0 commit comments

Comments
 (0)