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) { 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); } /** diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php index 9f98a2eaf..9ecf0e1a4 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' => 'bar', + 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, + )); + + $this->assertSame('foo', $fooRegion->getCache()->getNamespace()); + $this->assertSame('bar', $barRegion->getCache()->getNamespace()); + } } \ No newline at end of file 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