8000 [Cache] give control over cache prefix seed · symfony/symfony@17166b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17166b9

Browse files
committed
[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 2460ca5 commit 17166b9

File tree

9 files changed

+18
-10
lines changed

9 files changed

+18
-10
lines changed

UPGRADE-5.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ FrameworkBundle
5252
* Deprecated passing a `RouteCollectionBuilder` to `MicroKernelTrait::configureRoutes()`, type-hint `RoutingConfigurator` instead
5353
* Deprecated *not* setting the "framework.router.utf8" configuration option as it will default to `true` in Symfony 6.0
5454
* Deprecated `session.attribute_bag` service and `session.flash_bag` service.
55+
* If you configured the `framework.cache.prefix_seed` option, you might want to add the `%kernel.environment%` to its value to
56+
keep cache namespaces separated by environment of the app. The `%kernel.container_class%` (which includes the environment)
57+
used to be added by default to the seed, which is not the case anymore. This allows to share caches between
58+
apps or different environments.
5559

5660
HttpFoundation
5761
--------------

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,12 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName,
334334
if (!isset($cacheDriver['namespace'])) {
335335
// generate a unique namespace for the given application
336336
if ($container->hasParameter('cache.prefix.seed')) {
337-
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
337+
$seed = $container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
338338
} else {
339339
$seed = '_'.$container->getParameter('kernel.project_dir');
340+
$seed .= '.'.$container->getParameter('kernel.container_class');
340341
}
341-
$seed .= '.'.$container->getParameter('kernel.container_class');
342+
342343
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);
343344

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,8 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
871871
->children()
872872
->scalarNode('prefix_seed')
873873
->info('Used to namespace cache keys when using several apps with the same shared backend')
874-
->example('my-application-name')
874+
->defaultValue('_%kernel.project_dir%.%kernel.container_class%')
875+
->example('my-application-name/%kernel.environment%')
875876
->end()
876877
->scalarNode('app')
877878
->info('App related cache pools configuration')

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ protected static function getBundleDefaultConfig()
452452
'default_redis_provider' => 'redis://localhost',
453453
'default_memcached_provider' => 'memcached://localhost',
454454
'default_pdo_provider' => class_exists(Connection::class) ? 'database_connection' : null,
455+
'prefix_seed' => '_%kernel.project_dir%.%kernel.container_class%',
455456
],
456457
'workflows' => [
457458
'enabled' => false,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,11 +1270,11 @@ public function testCachePoolServices()
12701270
(new ChildDefinition('cache.adapter.array'))
12711271
->replaceArgument(0, 12),
12721272
(new ChildDefinition('cache.adapter.filesystem'))
1273-
->replaceArgument(0, 'xctxZ1lyiH')
1273+
->replaceArgument(0, 'UKoP1K+Hox')
12741274
->replaceArgument(1, 12),
12751275
(new ChildDefinition('cache.adapter.redis'))
12761276
->replaceArgument(0, new Reference('.cache_connection.kYdiLgf'))
1277-
->replaceArgument(1, 'xctxZ1lyiH')
1277+
->replaceArgument(1, 'UKoP1K+Hox')
12781278
->replaceArgument(2, 12),
12791279
],
12801280
12,

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
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.1",
2222
"symfony/config": "^5.0",
2323
"symfony/dependency-injection": "^5.1",
2424
"symfony/error-handler": "^4.4.1|^5.0.1",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ public function testRememberMeCookieInheritFrameworkSessionCookie($config, $same
348348
$container->setParameter('kernel.bundles_metadata', []);
349349
$container->setParameter('kernel.project_dir', __DIR__);
350350
$container->setParameter('kernel.cache_dir', __DIR__);
351+
$container->setParameter('kernel.container_class', 'app');
351352

352353
$container->loadFromExtension('security', [
353354
'firewalls' => [

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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)
0