10000 [Cache] give control over cache prefix seed · symfony/symfony@947e2bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 947e2bb

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 ad94b39 commit 947e2bb

File tree

9 files changed

+22
-10
lines changed

9 files changed

+22
-10
lines changed

UPGRADE-5.2.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ DependencyInjection
66

77
* Deprecated `Definition::setPrivate()` and `Alias::setPrivate()`, use `setPublic()` instead
88

9+
FrameworkBundle
10+
---------------
11+
12+
* If you configured the `framework.cache.prefix_seed` option, you might want to add the `%kernel.environment%` to its value to
13+
keep cache namespaces separated by environment of the app. The `%kernel.container_class%` (which includes the environment)
14+
used to be added by default to the seed, which is not the case anymore. This allows sharing caches between
15+
apps or different environments.
16+
917
Mime
1018
----
1119

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
911911
->children()
912912
->scalarNode('prefix_seed')
913913
->info('Used to namespace cache keys when using several apps with the same shared backend')
914-
->example('my-application-name')
914+
->defaultValue('_%kernel.project_dir%.%kernel.container_class%')
915+
->example('my-application-name/%kernel.environment%')
915916
->end()
916917
->scalarNode('app')
917918
->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
@@ -1269,11 +1269,11 @@ public function testCachePoolServices()
12691269
(new ChildDefinition('cache.adapter.array'))
12701270
->replaceArgument(0, 12),
12711271
(new ChildDefinition('cache.adapter.filesystem'))
1272-
->replaceArgument(0, 'xctxZ1lyiH')
1272+
->replaceArgument(0, 'UKoP1K+Hox')
12731273
->replaceArgument(1, 12),
12741274
(new ChildDefinition('cache.adapter.redis'))
12751275
->replaceArgument(0, new Reference('.cache_connection.kYdiLgf'))
1276-
->replaceArgument(1, 'xctxZ1lyiH')
1276+
->replaceArgument(1, 'UKoP1K+Hox')
12771277
->replaceArgument(2, 12),
12781278
],
12791279
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.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

Lines changed: 1 addition & 0 deletions
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

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