Skip to content

Commit

Permalink
Appends cache namespace when it exists (for L2C regions)
Browse files Browse the repository at this point in the history
We're overriding the namespace without even checking if it was previously
set, what causes problems when people uses that feature 😉
  • Loading branch information
lcobucci committed Jan 11, 2017
1 parent 4e57303 commit a722785
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
35 changes: 27 additions & 8 deletions lib/Doctrine/ORM/Cache/DefaultCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,9 @@ public function getRegion(array $cache)
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)
Expand All @@ -229,6 +224,30 @@ public function getRegion(array $cache)
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 (!empty($namespace)) {
$namespace .= ':';
}

$cacheAdapter->setNamespace($namespace . $name);

return $cacheAdapter;
}

/**
* {@inheritdoc}
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,30 @@ public function testBuildsNewNamespacedCacheInstancePerRegionInstance()
$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 */
Expand Down

0 comments on commit a722785

Please sign in to comment.