Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appends cache namespace when it exists (for L2C regions) #6223

Merged
merged 2 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ('' !== $namespace) {
$namespace .= ':';
}

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

return $cacheAdapter;
}

/**
* {@inheritdoc}
*/
Expand Down
126 changes: 75 additions & 51 deletions tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ protected function setUp()
$this->enableSecondLevelCache();
parent::setUp();

$this->em = $this->_getTestEntityManager();
$this->regionsConfig = new RegionsConfiguration;
$arguments = [$this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()];
$this->factory = $this->getMockBuilder(DefaultCacheFactory::class)
->setMethods(['getRegion'])
->setConstructorArgs($arguments)
->getMock();
$this->em = $this->_getTestEntityManager();
$this->regionsConfig = new RegionsConfiguration;
$arguments = [$this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()];
$this->factory = $this->getMockBuilder(DefaultCacheFactory::class)
->setMethods(['getRegion'])
->setConstructorArgs($arguments)
->getMock();
}

public function testImplementsCacheFactory()
Expand All @@ -68,10 +68,10 @@ public function testImplementsCacheFactory()

public function testBuildCachedEntityPersisterReadOnly()
{
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));

$metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_READ_ONLY;

Expand All @@ -88,10 +88,10 @@ public function testBuildCachedEntityPersisterReadOnly()

public function testBuildCachedEntityPersisterReadWrite()
{
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));

$metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_READ_WRITE;

Expand All @@ -108,10 +108,10 @@ public function testBuildCachedEntityPersisterReadWrite()

public function testBuildCachedEntityPersisterNonStrictReadWrite()
{
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));

$metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE;

Expand All @@ -128,11 +128,11 @@ public function testBuildCachedEntityPersisterNonStrictReadWrite()

public function testBuildCachedCollectionPersisterReadOnly()
{
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));

$mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_READ_ONLY;

Expand All @@ -150,11 +150,11 @@ public function testBuildCachedCollectionPersisterReadOnly()

public function testBuildCachedCollectionPersisterReadWrite()
{
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));

$mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_READ_WRITE;

Expand All @@ -171,11 +171,11 @@ public function testBuildCachedCollectionPersisterReadWrite()

public function testBuildCachedCollectionPersisterNonStrictReadWrite()
{
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));

$mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE;

Expand Down Expand Up @@ -234,9 +234,9 @@ public function testCreateNewCacheDriver()
*/
public function testBuildCachedEntityPersisterNonStrictException()
{
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);
$em = $this->em;
$metadata = clone $em->getClassMetadata(State::class);
$persister = new BasicEntityPersister($em, $metadata);

$metadata->cache['usage'] = -1;

Expand All @@ -249,10 +249,10 @@ public function testBuildCachedEntityPersisterNonStrictException()
*/
public function testBuildCachedCollectionPersisterException()
{
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);
$em = $this->em;
$metadata = $em->getClassMetadata(State::class);
$mapping = $metadata->associationMappings['cities'];
$persister = new OneToManyPersister($em);

$mapping['cache']['usage'] = -1;

Expand All @@ -269,8 +269,8 @@ public function testInvalidFileLockRegionDirectoryException()

$factory->getRegion(
[
'usage' => ClassMetadata::CACHE_USAGE_READ_WRITE,
'region' => 'foo'
'usage' => ClassMetadata::CACHE_USAGE_READ_WRITE,
'region' => 'foo'
]
);
}
Expand All @@ -281,21 +281,45 @@ public function testBuildsNewNamespacedCacheInstancePerRegionInstance()

$fooRegion = $factory->getRegion(
[
'region' => 'foo',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
'region' => 'foo',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
]
);
$barRegion = $factory->getRegion(
[
'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
]
);

$this->assertSame('foo', $fooRegion->getCache()->getNamespace());
$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(
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor alignment issues here

'region' => 'foo',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
]
);
$barRegion = $factory->getRegion(
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment issues here

'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
]
);

$this->assertSame('testing:foo', $fooRegion->getCache()->getNamespace());
$this->assertSame('testing:bar', $barRegion->getCache()->getNamespace());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are prefixes for non-namespaced caches checked in some existing tests??

Copy link
Member Author

@lcobucci lcobucci Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public function testBuildsDefaultCacheRegionFromGenericCacheRegion()
{
/* @var $cache \Doctrine\Common\Cache\Cache */
Expand All @@ -307,8 +331,8 @@ public function testBuildsDefaultCacheRegionFromGenericCacheRegion()
DefaultRegion::class,
$factory->getRegion(
[
'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
]
)
);
Expand All @@ -325,8 +349,8 @@ public function testBuildsMultiGetCacheRegionFromGenericCacheRegion()
DefaultMultiGetRegion::class,
$factory->getRegion(
[
'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
'region' => 'bar',
'usage' => ClassMetadata::CACHE_USAGE_READ_ONLY,
]
)
);
Expand Down