diff --git a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php index 151009632..3bfccdf84 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php +++ b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php @@ -200,14 +200,9 @@ class DefaultCacheFactory implements CacheFactory return $this->regions[$cache['region']]; } - $cacheAdapter = clone $this->cache; - - if ($cacheAdapter instanceof CacheProvider) { - $cacheAdapter->setNamespace($cache['region']); - } - - $name = $cache['region']; - $lifetime = $this->regionsConfig->getLifetime($cache['region']); + $name = $cache['region']; + $cacheAdapter = $this->createRegionCache($name); + $lifetime = $this->regionsConfig->getLifetime($cache['region']); $region = ($cacheAdapter instanceof MultiGetCache) ? new DefaultMultiGetRegion($name, $cacheAdapter, $lifetime) @@ -229,6 +224,30 @@ class DefaultCacheFactory implements CacheFactory return $this->regions[$cache['region']] = $region; } + /** + * @param string $name + * + * @return CacheAdapter + */ + private function createRegionCache($name) + { + $cacheAdapter = clone $this->cache; + + if (!$cacheAdapter instanceof CacheProvider) { + return $cacheAdapter; + } + + $namespace = $cacheAdapter->getNamespace(); + + if ('' !== $namespace) { + $namespace .= ':'; + } + + $cacheAdapter->setNamespace($namespace . $name); + + return $cacheAdapter; + } + /** * {@inheritdoc} */ diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php index 2047e05b3..90922caed 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php @@ -296,6 +296,30 @@ class DefaultCacheFactoryTest extends OrmTestCase $this->assertSame('bar', $barRegion->getCache()->getNamespace()); } + public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet() + { + $cache = clone $this->getSharedSecondLevelCacheDriverImpl(); + $cache->setNamespace('testing'); + + $factory = new DefaultCacheFactory($this->regionsConfig, $cache); + + $fooRegion = $factory->getRegion( + [ + 'region' => 'foo', + 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, + ] + ); + $barRegion = $factory->getRegion( + [ + 'region' => 'bar', + 'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY, + ] + ); + + $this->assertSame('testing:foo', $fooRegion->getCache()->getNamespace()); + $this->assertSame('testing:bar', $barRegion->getCache()->getNamespace()); + } + public function testBuildsDefaultCacheRegionFromGenericCacheRegion() { /* @var $cache \Doctrine\Common\Cache\Cache */