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:
commit
dda42f6c09
@ -123,31 +123,7 @@ class Setup
|
|||||||
{
|
{
|
||||||
$proxyDir = $proxyDir ?: sys_get_temp_dir();
|
$proxyDir = $proxyDir ?: sys_get_temp_dir();
|
||||||
|
|
||||||
if ($isDevMode === false && $cache === null) {
|
$cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache);
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
$config = new Configuration();
|
$config = new Configuration();
|
||||||
$config->setMetadataCacheImpl($cache);
|
$config->setMetadataCacheImpl($cache);
|
||||||
@ -159,4 +135,64 @@ class Setup
|
|||||||
|
|
||||||
return $config;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,43 @@ class SetupTest extends OrmTestCase
|
|||||||
$this->assertInstanceOf(YamlDriver::class, $config->getMetadataDriverImpl());
|
$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
|
* @group DDC-1350
|
||||||
*/
|
*/
|
||||||
@ -104,7 +141,7 @@ class SetupTest extends OrmTestCase
|
|||||||
public function testConfigureCacheCustomInstance()
|
public function testConfigureCacheCustomInstance()
|
||||||
{
|
{
|
||||||
$cache = $this->createMock(Cache::class);
|
$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->getResultCacheImpl());
|
||||||
$this->assertSame($cache, $config->getMetadataCacheImpl());
|
$this->assertSame($cache, $config->getMetadataCacheImpl());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user