From 94b1b12b55f400a9b38e3307375311a06db9fea6 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Wed, 2 Aug 2017 15:53:45 +0200 Subject: [PATCH] Hash cache keys on save --- src/Symfony/Component/Cache/Adapter/AbstractAdapter.php | 9 +++++---- .../Cache/Tests/Adapter/PhpArrayAdapterTest.php | 1 + .../Component/Cache/Tests/Simple/PhpArrayCacheTest.php | 3 +++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index 26b33e683e..9bc05fd2b3 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -55,19 +55,20 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface 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); } } diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php index ae0edb7d11..134dba7c90 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php @@ -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.', diff --git a/src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php b/src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php index 3016ac560e..57361905f8 100644 --- a/src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php +++ b/src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php @@ -21,6 +21,8 @@ use Symfony\Component\Cache\Simple\PhpArrayCache; 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', @@ -57,6 +59,7 @@ class PhpArrayCacheTest extends CacheTestCase FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache'); } } + public function createSimpleCache() { return new PhpArrayCacheWrapper(self::$file, new NullCache());