1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Merge pull request #6848 from lcobucci/cache-namespace-fix

Fix overwriting explicit cache namespace

Replaces: https://github.com/doctrine/doctrine2/pull/5904
This commit is contained in:
Luís Cobucci 2017-11-26 13:22:24 +01:00 committed by GitHub
commit dda42f6c09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 26 deletions

View File

@ -123,31 +123,7 @@ class Setup
{
$proxyDir = $proxyDir ?: sys_get_temp_dir();
if ($isDevMode === false && $cache === null) {
if (extension_loaded('apc')) {
$cache = new \Doctrine\Common\Cache\ApcCache();
} elseif (ini_get('xcache.cacher')) {
$cache = new \Doctrine\Common\Cache\XcacheCache();
} elseif (extension_loaded('memcache')) {
$memcache = new \Memcache();
$memcache->connect('127.0.0.1');
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$cache->setMemcache($memcache);
} elseif (extension_loaded('redis')) {
$redis = new \Redis();
$redis->connect('127.0.0.1');
$cache = new \Doctrine\Common\Cache\RedisCache();
$cache->setRedis($redis);
} else {
$cache = new ArrayCache();
}
} elseif ($cache === null) {
$cache = new ArrayCache();
}
if ($cache instanceof CacheProvider) {
$cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions
}
$cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache);
$config = new Configuration();
$config->setMetadataCacheImpl($cache);
@ -159,4 +135,64 @@ class Setup
return $config;
}
private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, ?Cache $cache) : Cache
{
$cache = self::createCacheInstance($isDevMode, $cache);
if ( ! $cache instanceof CacheProvider) {
return $cache;
}
$namespace = $cache->getNamespace();
if ($namespace !== '') {
$namespace .= ':';
}
$cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions
return $cache;
}
private static function createCacheInstance(bool $isDevMode, ?Cache $cache) : Cache
{
if ($cache !== null) {
return $cache;
}
if ($isDevMode === true) {
return new ArrayCache();
}
if (extension_loaded('apc')) {
return new \Doctrine\Common\Cache\ApcCache();
}
if (ini_get('xcache.cacher')) {
return new \Doctrine\Common\Cache\XcacheCache();
}
if (extension_loaded('memcache')) {
$memcache = new \Memcache();
$memcache->connect('127.0.0.1');
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$cache->setMemcache($memcache);
return $cache;
}
if (extension_loaded('redis')) {
$redis = new \Redis();
$redis->connect('127.0.0.1');
$cache = new \Doctrine\Common\Cache\RedisCache();
$cache->setRedis($redis);
return $cache;
}
return new ArrayCache();
}
}

View File

@ -76,6 +76,43 @@ class SetupTest extends OrmTestCase
$this->assertInstanceOf(YamlDriver::class, $config->getMetadataDriverImpl());
}
/**
* @group 5904
*/
public function testCacheNamespaceShouldBeGeneratedWhenCacheIsNotGiven() : void
{
$config = Setup::createConfiguration(false, '/foo');
$cache = $config->getMetadataCacheImpl();
self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf_', $cache->getNamespace());
}
/**
* @group 5904
*/
public function testCacheNamespaceShouldBeGeneratedWhenCacheIsGivenButHasNoNamespace() : void
{
$config = Setup::createConfiguration(false, '/foo', new ArrayCache());
$cache = $config->getMetadataCacheImpl();
self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf_', $cache->getNamespace());
}
/**
* @group 5904
*/
public function testConfiguredCacheNamespaceShouldBeUsedAsPrefixOfGeneratedNamespace() : void
{
$originalCache = new ArrayCache();
$originalCache->setNamespace('foo');
$config = Setup::createConfiguration(false, '/foo', $originalCache);
$cache = $config->getMetadataCacheImpl();
self::assertSame($originalCache, $cache);
self::assertSame('foo:dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf_', $cache->getNamespace());
}
/**
* @group DDC-1350
*/
@ -104,7 +141,7 @@ class SetupTest extends OrmTestCase
public function testConfigureCacheCustomInstance()
{
$cache = $this->createMock(Cache::class);
$config = Setup::createConfiguration([], true, $cache);
$config = Setup::createConfiguration(true, null, $cache);
$this->assertSame($cache, $config->getResultCacheImpl());
$this->assertSame($cache, $config->getMetadataCacheImpl());