[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.
This commit is contained in:
Tobias Schultze 2020-02-14 17:09:07 +01:00 committed by Nicolas Grekas
parent a64b9449a3
commit 6681b92524
9 changed files with 18 additions and 10 deletions

View File

@ -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
----

View File

@ -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');
}
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);
$cacheDriver['namespace'] = $namespace;

View File

@ -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')

View File

@ -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,

View File

@ -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,

View File

@ -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",

View File

@ -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' => [

View File

@ -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');
}
$allPools = [];
$clearers = [];

View File

@ -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()