From f38d4551f14ecd2b19b4e0ff6551d74064c54591 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Jan 2015 21:01:02 +0100 Subject: [PATCH 1/5] Cache regions should not modify the injected cache instance settings --- .../Doctrine/Tests/ORM/Cache/DefaultRegionTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php index e8f7764b5..f449cfe6d 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM\Cache; +use Doctrine\Common\Cache\ArrayCache; use Doctrine\ORM\Cache\Region\DefaultRegion; use Doctrine\Tests\Mocks\CacheEntryMock; use Doctrine\Tests\Mocks\CacheKeyMock; @@ -47,4 +48,16 @@ class DefaultRegionTest extends AbstractRegionTest $this->assertFalse($region1->contains($key)); $this->assertTrue($region2->contains($key)); } + + public function testDoesNotModifyCacheNamespace() + { + $cache = new ArrayCache(); + + $cache->setNamespace('foo'); + + new DefaultRegion('bar', $cache); + new DefaultRegion('baz', $cache); + + $this->assertSame('foo', $cache->getNamespace()); + } } \ No newline at end of file From 514fd008b906548182afd224b1aefaffb37c7096 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Jan 2015 21:01:35 +0100 Subject: [PATCH 2/5] Cache regions will not modify the injected cache instance settings --- lib/Doctrine/ORM/Cache/Region/DefaultRegion.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php b/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php index 42e1a3471..a1362989f 100644 --- a/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php +++ b/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php @@ -59,8 +59,6 @@ class DefaultRegion implements Region $this->cache = $cache; $this->name = (string) $name; $this->lifetime = (integer) $lifetime; - - $this->cache->setNamespace($this->name); } /** From a82eecfc070cc83315fc57f3e980b9b711872f73 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Jan 2015 21:15:38 +0100 Subject: [PATCH 3/5] Each cache region built by the `DefaultCacheFactory` should have its own cache with its own unique namespace --- .../ORM/Cache/DefaultCacheFactoryTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php index 9f98a2eaf..59eb87085 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php @@ -257,11 +257,28 @@ class DefaultCacheFactoryTest extends OrmTestCase */ public function testInvalidFileLockRegionDirectoryException() { - $factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); + $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); $factory->getRegion(array( 'usage' => ClassMetadata::CACHE_USAGE_READ_WRITE, 'region' => 'foo' )); } + + public function testBuildsNewNamespacedCacheInstancePerRegionInstance() + { + $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); + + $fooRegion = $factory->getRegion(array( + 'region' => 'foo', + 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, + )); + $barRegion = $factory->getRegion(array( + 'region' => 'foo', + 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, + )); + + $this->assertSame('foo', $fooRegion->getCache()->getNamespace()); + $this->assertSame('bar', $barRegion->getCache()->getNamespace()); + } } \ No newline at end of file From 6564f952606f53fef9ff462bc80add23c372b32c Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Jan 2015 21:17:25 +0100 Subject: [PATCH 4/5] Typo fix (region should be named `'bar'`, not `'foo'` --- tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php index 59eb87085..9ecf0e1a4 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php @@ -274,7 +274,7 @@ class DefaultCacheFactoryTest extends OrmTestCase 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, )); $barRegion = $factory->getRegion(array( - 'region' => 'foo', + 'region' => 'bar', 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, )); From 0f2c117f3ed2e2af03c50d6e1ce7ae65aaf4c41d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Jan 2015 21:17:49 +0100 Subject: [PATCH 5/5] Cloning and configuring the cache adapter for each newly created cache region (fixes cache namespacing) --- lib/Doctrine/ORM/Cache/DefaultCacheFactory.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php index 207c34a61..6e93c6ecf 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php +++ b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php @@ -198,7 +198,11 @@ class DefaultCacheFactory implements CacheFactory return $this->regions[$cache['region']]; } - $region = new DefaultRegion($cache['region'], clone $this->cache, $this->regionsConfig->getLifetime($cache['region'])); + $cacheAdapter = clone $this->cache; + + $cacheAdapter->setNamespace($cache['region']); + + $region = new DefaultRegion($cache['region'], $cacheAdapter, $this->regionsConfig->getLifetime($cache['region'])); if ($cache['usage'] === ClassMetadata::CACHE_USAGE_READ_WRITE) {