Skip to content

Commit 6681b92

Browse files
Tobionnicolas-grekas
authored andcommitted
[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.
1 parent a64b944 commit 6681b92

File tree

9 files changed

+18
-10
lines changed

9 files changed

+18
-10
lines changed

UPGRADE-5.2.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ FrameworkBundle
1111

1212
* Deprecated the public `form.factory`, `form.type.file`, `translator`, `security.csrf.token_manager`, `serializer`,
1313
`cache_clearer`, `filesystem` and `validator` services to private.
14+
* If you configured the `framework.cache.prefix_seed` option, you might want to add the `%kernel.environment%` to its value to
15+
keep cache namespaces separated by environment of the app. The `%kernel.container_class%` (which includes the environment)
16+
used to be added by default to the seed, which is not the case anymore. This allows sharing caches between
17+
apps or different environments.
1418

1519
Mime
1620
----

src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName,
332332
if (!isset($cacheDriver['namespace'])) {
333333
// generate a unique namespace for the given application
334334
if ($container->hasParameter('cache.prefix.seed')) {
335-
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
335+
$seed = $container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
336336
} else {
337337
$seed = '_'.$container->getParameter('kernel.project_dir');
338+
$seed .= '.'.$container->getParameter('kernel.container_class');
338339
}
339-
$seed .= '.'.$container->getParameter('kernel.container_class');
340+
340341
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);
341342

342343
$cacheDriver['namespace'] = $namespace;

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
982982
->children()
983983
->scalarNode('prefix_seed')
984984
->info('Used to namespace cache keys when using several apps with the same shared backend')
985-
->example('my-application-name')
985+
->defaultValue('_%kernel.project_dir%.%kernel.container_class%')
986+
->example('my-application-name/%kernel.environment%')
986987
->end()
987988
->scalarNode('app')
988989
->info('App related cache pools configuration')

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ protected static function getBundleDefaultConfig()
463463
'default_redis_provider' => 'redis://localhost',
464464
'default_memcached_provider' => 'memcached://localhost',
465465
'default_pdo_provider' => class_exists(Connection::class) ? 'database_connection' : null,
466+
'prefix_seed' => '_%kernel.project_dir%.%kernel.container_class%',
466467
],
467468
'workflows' => [
468469
'enabled' => false,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1334,11 +1334,11 @@ public function testCachePoolServices()
13341334
(new ChildDefinition('cache.adapter.array'))
13351335
->replaceArgument(0, 12),
13361336
(new ChildDefinition('cache.adapter.filesystem'))
1337-
->replaceArgument(0, 'xctxZ1lyiH')
1337+
->replaceArgument(0, 'UKoP1K+Hox')
13381338
->replaceArgument(1, 12),
13391339
(new ChildDefinition('cache.adapter.redis'))
13401340
->replaceArgument(0, new Reference('.cache_connection.kYdiLgf'))
1341-
->replaceArgument(1, 'xctxZ1lyiH')
1341+
->replaceArgument(1, 'UKoP1K+Hox')
13421342
->replaceArgument(2, 12),
13431343
],
13441344
12,

src/Symfony/Bundle/FrameworkBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=7.2.5",
2020
"ext-xml": "*",
21-
"symfony/cache": "^4.4|^5.0",
21+
"symfony/cache": "^5.2",
2222
"symfony/config": "^5.0",
2323
"symfony/dependency-injection": "^5.2",
2424
"symfony/event-dispatcher": "^5.1",

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ public function testRememberMeCookieInheritFrameworkSessionCookie($config, $same
365365
$container->setParameter('kernel.bundles_metadata', []);
366366
$container->setParameter('kernel.project_dir', __DIR__);
367367
$container->setParameter('kernel.cache_dir', __DIR__);
368+
$container->setParameter('kernel.container_class', 'app');
368369

369370
$container->loadFromExtension('security', [
370371
'firewalls' => [

src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public function __construct(string $cachePoolTag = 'cache.pool', string $kernelR
4949
public function process(ContainerBuilder $container)
5050
{
5151
if ($container->hasParameter('cache.prefix.seed')) {
52-
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
52+
$seed = $container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
5353
} else {
5454
$seed = '_'.$container->getParameter('kernel.project_dir');
55+
$seed .= '.'.$container->getParameter('kernel.container_class');
5556
}
56-
$seed .= '.'.$container->getParameter('kernel.container_class');
5757

5858
$allPools = [];
5959
$clearers = [];

src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testArgsAreReplaced()
135135

136136
$this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
137137
$this->assertSame('foobar', (string) $cachePool->getArgument(0));
138-
$this->assertSame('tQNhcV-8xa', $cachePool->getArgument(1));
138+
$this->assertSame('6Ridbw4aMn', $cachePool->getArgument(1));
139139
$this->assertSame(3, $cachePool->getArgument(2));
140140
}
141141

@@ -156,7 +156,7 @@ public function testWithNameAttribute()
156156

157157
$this->cachePoolPass->process($container);
158158

159-
$this->assertSame('+naTpPa4Sm', $cachePool->getArgument(1));
159+
$this->assertSame('PeXBWSl6ca', $cachePool->getArgument(1));
160160
}
161161

162162
public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()

0 commit comments

Comments
 (0)