Skip to content

Commit

Permalink
bug #23763 [Cache] Hash cache key on save (lstrojny)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolas-grekas committed Aug 7, 2017
2 parents 99806c5 + 94b1b12 commit 267b016
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Expand Up @@ -55,19 +55,20 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
null,
CacheItem::class
);
$getId = function ($key) { return $this->getId((string) $key); };
$this->mergeByLifetime = \Closure::bind(
function ($deferred, $namespace, &$expiredIds) {
function ($deferred, $namespace, &$expiredIds) use ($getId) {
$byLifetime = array();
$now = time();
$expiredIds = array();

foreach ($deferred as $key => $item) {
if (null === $item->expiry) {
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$namespace.$key] = $item->value;
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$getId($key)] = $item->value;
} elseif ($item->expiry > $now) {
$byLifetime[$item->expiry - $now][$namespace.$key] = $item->value;
$byLifetime[$item->expiry - $now][$getId($key)] = $item->value;
} else {
$expiredIds[] = $namespace.$key;
$expiredIds[] = $getId($key);
}
}

Expand Down
Expand Up @@ -22,6 +22,7 @@ class PhpArrayAdapterTest extends AdapterTestCase
{
protected $skippedTests = array(
'testBasicUsage' => 'PhpArrayAdapter is read-only.',
'testBasicUsageWithLongKey' => 'PhpArrayAdapter is read-only.',
'testClear' => 'PhpArrayAdapter is read-only.',
'testClearWithDeferredItems' => 'PhpArrayAdapter is read-only.',
'testDeleteItem' => 'PhpArrayAdapter is read-only.',
Expand Down
Expand Up @@ -21,6 +21,8 @@
class PhpArrayCacheTest extends CacheTestCase
{
protected $skippedTests = array(
'testBasicUsageWithLongKey' => 'PhpArrayCache does no writes',

'testDelete' => 'PhpArrayCache does no writes',
'testDeleteMultiple' => 'PhpArrayCache does no writes',
'testDeleteMultipleGenerator' => 'PhpArrayCache does no writes',
Expand Down Expand Up @@ -57,6 +59,7 @@ protected function tearDown()
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
}

public function createSimpleCache()
{
return new PhpArrayCacheWrapper(self::$file, new NullCache());
Expand Down

0 comments on commit 267b016

Please sign in to comment.