Skip to content

[Cache] give control over cache prefix seed #35890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UPGRADE-5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName,
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
->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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ protected static function getBundleDefaultConfig()
'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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1334,11 +1334,11 @@ public function testCachePoolServices()
(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,
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public function testRememberMeCookieInheritFrameworkSessionCookie($config, $same
$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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function __construct(string $cachePoolTag = 'cache.pool', string $kernelR
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 = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testArgsAreReplaced()

$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));
}

Expand All @@ -156,7 +156,7 @@ public function testWithNameAttribute()

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

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

public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
Expand Down