Skip to content

Commit 267b016

Browse files
bug symfony#23763 [Cache] Hash cache key on save (lstrojny)
This PR was merged into the 3.3 branch. Discussion ---------- [Cache] Hash cache key on save | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | ye | Fixed tickets | n.A. | License | MIT | Doc PR | n.A. Cache keys are not hashed right now in adapters extending from `AbstractAdapter`. This PR fixes this. I am not familiar enough with the cache test suite so I don't know where to add an regression test. Commits ------- 94b1b12 Hash cache keys on save
2 parents 99806c5 + 94b1b12 commit 267b016

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
5555
null,
5656
CacheItem::class
5757
);
58+
$getId = function ($key) { return $this->getId((string) $key); };
5859
$this->mergeByLifetime = \Closure::bind(
59-
function ($deferred, $namespace, &$expiredIds) {
60+
function ($deferred, $namespace, &$expiredIds) use ($getId) {
6061
$byLifetime = array();
6162
$now = time();
6263
$expiredIds = array();
6364

6465
foreach ($deferred as $key => $item) {
6566
if (null === $item->expiry) {
66-
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$namespace.$key] = $item->value;
67+
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$getId($key)] = $item->value;
6768
} elseif ($item->expiry > $now) {
68-
$byLifetime[$item->expiry - $now][$namespace.$key] = $item->value;
69+
$byLifetime[$item->expiry - $now][$getId($key)] = $item->value;
6970
} else {
70-
$expiredIds[] = $namespace.$key;
71+
$expiredIds[] = $getId($key);
7172
}
7273
}
7374

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PhpArrayAdapterTest extends AdapterTestCase
2222
{
2323
protected $skippedTests = array(
2424
'testBasicUsage' => 'PhpArrayAdapter is read-only.',
25+
'testBasicUsageWithLongKey' => 'PhpArrayAdapter is read-only.',
2526
'testClear' => 'PhpArrayAdapter is read-only.',
2627
'testClearWithDeferredItems' => 'PhpArrayAdapter is read-only.',
2728
'testDeleteItem' => 'PhpArrayAdapter is read-only.',

src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
class PhpArrayCacheTest extends CacheTestCase
2222
{
2323
protected $skippedTests = array(
24+
'testBasicUsageWithLongKey' => 'PhpArrayCache does no writes',
25+
2426
'testDelete' => 'PhpArrayCache does no writes',
2527
'testDeleteMultiple' => 'PhpArrayCache does no writes',
2628
'testDeleteMultipleGenerator' => 'PhpArrayCache does no writes',
@@ -57,6 +59,7 @@ protected function tearDown()
5759
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
5860
}
5961
}
62+
6063
public function createSimpleCache()
6164
{
6265
return new PhpArrayCacheWrapper(self::$file, new NullCache());

0 commit comments

Comments
 (0)