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

Fix overwriting explicit cache namespace

This commit is contained in:
Jan Jakes 2016-06-28 10:34:43 +02:00 committed by Luís Cobucci
parent d3759a2447
commit 99b2e57606
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C
2 changed files with 98 additions and 25 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
*/