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 27, 2019
1 parent a374f3b commit 33b4cca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
24 changes: 11 additions & 13 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 @@ -893,11 +894,7 @@ private function loadMessengerServices(ContainerBuilder $container) : void

private function createPoolCacheDefinition(ContainerBuilder $container, string $poolName) : string
{
if (! class_exists(DoctrineProvider::class)) {
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.orm.cache.provider.%s', $poolName);

$definition = $container->register($serviceId, DoctrineProvider::class);
$definition->addArgument(new Reference($poolName));
Expand All @@ -906,13 +903,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.orm.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.orm.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.orm.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.orm.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.orm.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.orm.cache.provider.result_cache_pool',
'cacheName' => 'result_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'result_cache_pool'],
],
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -28,6 +28,7 @@
"doctrine/dbal": "^2.5.12",
"doctrine/doctrine-cache-bundle": "~1.2",
"jdorn/sql-formatter": "^1.2.16",
"symfony/cache": "^3.4.30|^4.3.3",
"symfony/config": "^3.4.30|^4.3.3",
"symfony/console": "^3.4.30|^4.3.3",
"symfony/dependency-injection": "^3.4.30|^4.3.3",
Expand All @@ -39,7 +40,6 @@
"doctrine/orm": "^2.6",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^7.5",
"symfony/cache": "^3.4.30|^4.3.3",
"symfony/phpunit-bridge": "^4.2",
"symfony/property-info": "^3.4.30|^4.3.3",
"symfony/twig-bridge": "^3.4|^4.1",
Expand Down

0 comments on commit 33b4cca

Please sign in to comment.