Skip to content

Commit

Permalink
Fix: use array adapter cache pools by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Nov 26, 2019
1 parent a374f3b commit f9143e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
20 changes: 11 additions & 9 deletions DependencyInjection/DoctrineExtension.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
Expand Down Expand Up @@ -743,7 +744,7 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD
if ($cacheDriver['type'] === null) {
$cacheDriver = [
'type' => 'pool',
'pool' => $this->getPoolNameForCacheDriver($cacheName),
'pool' => $this->createArrayAdapterCachePool($container, $objectManagerName, $cacheName),
];
}

Expand Down Expand Up @@ -897,7 +898,7 @@ private function createPoolCacheDefinition(ContainerBuilder $container, string $
throw new LogicException('Using the "pool" cache type is only supported when symfony/cache is installed.');
}

$serviceId = sprintf('doctrine.orm.cache.pool.%s', $poolName);
$serviceId = sprintf('doctrine.cache.provider.%s', $poolName);

$definition = $container->register($serviceId, DoctrineProvider::class);
$definition->addArgument(new Reference($poolName));
Expand All @@ -906,13 +907,14 @@ private function createPoolCacheDefinition(ContainerBuilder $container, string $
return $serviceId;
}

private function getPoolNameForCacheDriver(string $driverName) : string
private function createArrayAdapterCachePool(ContainerBuilder $container, string $objectManagerName, string $cacheName) : string
{
switch ($driverName) {
case 'metadata_cache':
return 'cache.system';
default:
return 'cache.app';
}
$id = sprintf('cache.doctrine.orm.%s.%s', $objectManagerName, str_replace('_cache', '', $cacheName));

$poolDefinition = $container->register($id, ArrayAdapter::class);
$poolDefinition->addTag('cache.pool');
$container->setDefinition($id, $poolDefinition);

return $id;
}
}
4 changes: 2 additions & 2 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Expand Up @@ -348,13 +348,13 @@ public function testLoadMultipleConnections()
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.app', (string) $arguments[0]);
$this->assertEquals('cache.doctrine.orm.em1.query', (string) $arguments[0]);

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_result_cache'));
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.app', (string) $arguments[0]);
$this->assertEquals('cache.doctrine.orm.em1.result', (string) $arguments[0]);
}

public function testLoadLogging()
Expand Down
21 changes: 12 additions & 9 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Expand Up @@ -326,19 +326,22 @@ public function testDependencyInjectionConfigurationDefaults()
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.system', (string) $arguments[0]);
$this->assertEquals('cache.doctrine.orm.default.metadata', (string) $arguments[0]);
$this->assertSame(ArrayAdapter::class, $container->getDefinition((string) $arguments[0])->getClass());

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_query_cache'));
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.app', (string) $arguments[0]);
$this->assertEquals('cache.doctrine.orm.default.query', (string) $arguments[0]);
$this->assertSame(ArrayAdapter::class, $container->getDefinition((string) $arguments[0])->getClass());

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_result_cache'));
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.app', (string) $arguments[0]);
$this->assertEquals('cache.doctrine.orm.default.result', (string) $arguments[0]);
$this->assertSame(ArrayAdapter::class, $container->getDefinition((string) $arguments[0])->getClass());
}

public function testUseSavePointsAddMethodCallToAddSavepointsToTheConnection()
Expand Down Expand Up @@ -775,38 +778,38 @@ public static function cacheConfigurationProvider() : array
return [
'metadata_cache_default' => [
'expectedAliasName' => 'doctrine.orm.default_metadata_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.pool.cache.system',
'expectedAliasTarget' => 'doctrine.cache.provider.cache.doctrine.orm.default.metadata',
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => null],
],
'query_cache_default' => [
'expectedAliasName' => 'doctrine.orm.default_query_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.pool.cache.app',
'expectedAliasTarget' => 'doctrine.cache.provider.cache.doctrine.orm.default.query',
'cacheName' => 'query_cache_driver',
'cacheConfig' => ['type' => null],
],
'result_cache_default' => [
'expectedAliasName' => 'doctrine.orm.default_result_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.pool.cache.app',
'expectedAliasTarget' => 'doctrine.cache.provider.cache.doctrine.orm.default.result',
'cacheName' => 'result_cache_driver',
'cacheConfig' => ['type' => null],
],

'metadata_cache_pool' => [
'expectedAliasName' => 'doctrine.orm.default_metadata_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.pool.metadata_cache_pool',
'expectedAliasTarget' => 'doctrine.cache.provider.metadata_cache_pool',
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'metadata_cache_pool'],
],
'query_cache_pool' => [
'expectedAliasName' => 'doctrine.orm.default_query_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.pool.query_cache_pool',
'expectedAliasTarget' => 'doctrine.cache.provider.query_cache_pool',
'cacheName' => 'query_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'query_cache_pool'],
],
'result_cache_pool' => [
'expectedAliasName' => 'doctrine.orm.default_result_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.pool.result_cache_pool',
'expectedAliasTarget' => 'doctrine.cache.provider.result_cache_pool',
'cacheName' => 'result_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'result_cache_pool'],
],
Expand Down

0 comments on commit f9143e3

Please sign in to comment.