Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use array adapter cache pools by default #1073

Merged
merged 1 commit into from Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every object-manager needs separate caches, right? Otherwise there could be weird key collisions I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the EM uses as key for the entry, but this is definitely safer than using shared caches.


$poolDefinition = $container->register($id, ArrayAdapter::class);
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
$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 @@ -29,6 +29,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 @@ -40,7 +41,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