From 6681b92524d069aa33de79458d0ff9b42aabd011 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 14 Feb 2020 17:09:07 +0100 Subject: [PATCH] [Cache] give control over cache prefix seed The configurable cache prefix seed does not give full control over the cache prefix because the container class is added to the prefix in any case. This is a problem because the container class contains the app env name. We use different app environments for different deployment targets (dev and test). Dev and test should use the same redis cache. But this is impossible to achieve because even setting the cache prefix seed does not accomplish this. --- UPGRADE-5.2.md | 4 ++++ .../DependencyInjection/AbstractDoctrineExtension.php | 5 +++-- .../FrameworkBundle/DependencyInjection/Configuration.php | 3 ++- .../Tests/DependencyInjection/ConfigurationTest.php | 1 + .../Tests/DependencyInjection/FrameworkExtensionTest.php | 4 ++-- src/Symfony/Bundle/FrameworkBundle/composer.json | 2 +- .../Tests/DependencyInjection/SecurityExtensionTest.php | 1 + .../Component/Cache/DependencyInjection/CachePoolPass.php | 4 ++-- .../Cache/Tests/DependencyInjection/CachePoolPassTest.php | 4 ++-- 9 files changed, 18 insertions(+), 10 deletions(-) diff --git a/UPGRADE-5.2.md b/UPGRADE-5.2.md index 01d1b7f9ff..ac5518d2ed 100644 --- a/UPGRADE-5.2.md +++ b/UPGRADE-5.2.md @@ -11,6 +11,10 @@ FrameworkBundle * Deprecated the public `form.factory`, `form.type.file`, `translator`, `security.csrf.token_manager`, `serializer`, `cache_clearer`, `filesystem` and `validator` services to private. + * If you configured the `framework.cache.prefix_seed` option, you might want to add the `%kernel.environment%` to its value to + keep cache namespaces separated by environment of the app. The `%kernel.container_class%` (which includes the environment) + used to be added by default to the seed, which is not the case anymore. This allows sharing caches between + apps or different environments. Mime ---- diff --git a/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php b/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php index c2705c73fa..ad94a37ff0 100644 --- a/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php +++ b/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php @@ -332,11 +332,12 @@ abstract class AbstractDoctrineExtension extends Extension if (!isset($cacheDriver['namespace'])) { // generate a unique namespace for the given application if ($container->hasParameter('cache.prefix.seed')) { - $seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed')); + $seed = $container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed')); } else { $seed = '_'.$container->getParameter('kernel.project_dir'); + $seed .= '.'.$container->getParameter('kernel.container_class'); } - $seed .= '.'.$container->getParameter('kernel.container_class'); + $namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed); $cacheDriver['namespace'] = $namespace; diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 085ceb5daf..0378470985 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -982,7 +982,8 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('prefix_seed') ->info('Used to namespace cache keys when using several apps with the same shared backend') - ->example('my-application-name') + ->defaultValue('_%kernel.project_dir%.%kernel.container_class%') + ->example('my-application-name/%kernel.environment%') ->end() ->scalarNode('app') ->info('App related cache pools configuration') diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index e47f19aea8..2c7920214c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -463,6 +463,7 @@ class ConfigurationTest extends TestCase 'default_redis_provider' => 'redis://localhost', 'default_memcached_provider' => 'memcached://localhost', 'default_pdo_provider' => class_exists(Connection::class) ? 'database_connection' : null, + 'prefix_seed' => '_%kernel.project_dir%.%kernel.container_class%', ], 'workflows' => [ 'enabled' => false, diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 99054524d0..672162376e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -1334,11 +1334,11 @@ abstract class FrameworkExtensionTest extends TestCase (new ChildDefinition('cache.adapter.array')) ->replaceArgument(0, 12), (new ChildDefinition('cache.adapter.filesystem')) - ->replaceArgument(0, 'xctxZ1lyiH') + ->replaceArgument(0, 'UKoP1K+Hox') ->replaceArgument(1, 12), (new ChildDefinition('cache.adapter.redis')) ->replaceArgument(0, new Reference('.cache_connection.kYdiLgf')) - ->replaceArgument(1, 'xctxZ1lyiH') + ->replaceArgument(1, 'UKoP1K+Hox') ->replaceArgument(2, 12), ], 12, diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index ec6f8888eb..59580d478c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -18,7 +18,7 @@ "require": { "php": ">=7.2.5", "ext-xml": "*", - "symfony/cache": "^4.4|^5.0", + "symfony/cache": "^5.2", "symfony/config": "^5.0", "symfony/dependency-injection": "^5.2", "symfony/event-dispatcher": "^5.1", diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php index 195e317417..3256b10461 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php @@ -365,6 +365,7 @@ class SecurityExtensionTest extends TestCase $container->setParameter('kernel.bundles_metadata', []); $container->setParameter('kernel.project_dir', __DIR__); $container->setParameter('kernel.cache_dir', __DIR__); + $container->setParameter('kernel.container_class', 'app'); $container->loadFromExtension('security', [ 'firewalls' => [ diff --git a/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php b/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php index f52d0271e4..fc78242b3a 100644 --- a/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php +++ b/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php @@ -49,11 +49,11 @@ class CachePoolPass implements CompilerPassInterface public function process(ContainerBuilder $container) { if ($container->hasParameter('cache.prefix.seed')) { - $seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed')); + $seed = $container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed')); } else { $seed = '_'.$container->getParameter('kernel.project_dir'); + $seed .= '.'.$container->getParameter('kernel.container_class'); } - $seed .= '.'.$container->getParameter('kernel.container_class'); $allPools = []; $clearers = []; diff --git a/src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php b/src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php index 20701adcb4..9c230837a5 100644 --- a/src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php +++ b/src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php @@ -135,7 +135,7 @@ class CachePoolPassTest extends TestCase $this->assertInstanceOf(Reference::class, $cachePool->getArgument(0)); $this->assertSame('foobar', (string) $cachePool->getArgument(0)); - $this->assertSame('tQNhcV-8xa', $cachePool->getArgument(1)); + $this->assertSame('6Ridbw4aMn', $cachePool->getArgument(1)); $this->assertSame(3, $cachePool->getArgument(2)); } @@ -156,7 +156,7 @@ class CachePoolPassTest extends TestCase $this->cachePoolPass->process($container); - $this->assertSame('+naTpPa4Sm', $cachePool->getArgument(1)); + $this->assertSame('PeXBWSl6ca', $cachePool->getArgument(1)); } public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()