Skip to content

Commit a4171cf

Browse files
bug #20616 [Bridge/Doctrine] Use cache.prefix.seed parameter for generating cache namespace (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [Bridge/Doctrine] Use cache.prefix.seed parameter for generating cache namespace | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Exactly the same issue as in #20610, but for Doctrine ORM's cache: > That's a design issue: using root_dir as discriminant doesn't work with blue/green deployment strategies, and doesn't prevent collision when different apps are deployed in the same path while sharing the same cache backend. Commits ------- 5e3cbec [Bridge/Doctrine] Use cache.prefix.seed parameter for generating cache namespace
2 parents ec2f4f1 + 1e55e69 commit a4171cf

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

DependencyInjection/Compiler/CachePoolClearerPass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ final class CachePoolClearerPass implements CompilerPassInterface
2525
*/
2626
public function process(ContainerBuilder $container)
2727
{
28+
$container->getParameterBag()->remove('cache.prefix.seed');
29+
2830
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
2931
foreach (array_reverse($attributes) as $attr) {
3032
if (isset($attr['clearer'])) {

DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ class CachePoolPass implements CompilerPassInterface
2929
*/
3030
public function process(ContainerBuilder $container)
3131
{
32-
$namespaceSuffix = '';
33-
34-
foreach (array('kernel.name', 'kernel.environment', 'kernel.debug', 'cache.prefix.seed') as $key) {
35-
if ($container->hasParameter($key)) {
36-
$namespaceSuffix .= '.'.$container->getParameter($key);
37-
}
32+
if ($container->hasParameter('cache.prefix.seed')) {
33+
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
34+
} else {
35+
$seed = '_'.$container->getParameter('kernel.root_dir');
3836
}
39-
$container->getParameterBag()->remove('cache.prefix.seed');
37+
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');
4038

4139
$aliases = $container->getAliases();
4240
$attributes = array(
@@ -56,7 +54,7 @@ public function process(ContainerBuilder $container)
5654
}
5755
}
5856
if (!isset($tags[0]['namespace'])) {
59-
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, $id);
57+
$tags[0]['namespace'] = $this->getNamespace($seed, $id);
6058
}
6159
if (isset($tags[0]['clearer'])) {
6260
$clearer = strtolower($tags[0]['clearer']);
@@ -88,9 +86,9 @@ public function process(ContainerBuilder $container)
8886
}
8987
}
9088

91-
private function getNamespace($namespaceSuffix, $id)
89+
private function getNamespace($seed, $id)
9290
{
93-
return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$namespaceSuffix, true))), 0, 10);
91+
return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10);
9492
}
9593

9694
/**

Tests/DependencyInjection/Compiler/CachePoolClearerPassTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class CachePoolClearerPassTest extends \PHPUnit_Framework_TestCase
2424
public function testPoolRefsAreWeak()
2525
{
2626
$container = new ContainerBuilder();
27+
$container->setParameter('kernel.debug', false);
28+
$container->setParameter('kernel.name', 'app');
29+
$container->setParameter('kernel.environment', 'prod');
30+
$container->setParameter('kernel.root_dir', 'foo');
2731

2832
$publicPool = new Definition();
2933
$publicPool->addArgument('namespace');

Tests/DependencyInjection/Compiler/CachePoolPassTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ protected function setUp()
2929
public function testNamespaceArgumentIsReplaced()
3030
{
3131
$container = new ContainerBuilder();
32+
$container->setParameter('kernel.debug', false);
33+
$container->setParameter('kernel.name', 'app');
34+
$container->setParameter('kernel.environment', 'prod');
35+
$container->setParameter('kernel.root_dir', 'foo');
3236
$adapter = new Definition();
3337
$adapter->setAbstract(true);
3438
$adapter->addTag('cache.pool');
@@ -41,12 +45,16 @@ public function testNamespaceArgumentIsReplaced()
4145

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

44-
$this->assertSame('kRFqMp5odS', $cachePool->getArgument(0));
48+
$this->assertSame('C42Pcl9VBJ', $cachePool->getArgument(0));
4549
}
4650

4751
public function testArgsAreReplaced()
4852
{
4953
$container = new ContainerBuilder();
54+
$container->setParameter('kernel.debug', false);
55+
$container->setParameter('kernel.name', 'app');
56+
$container->setParameter('kernel.environment', 'prod');
57+
$container->setParameter('cache.prefix.seed', 'foo');
5058
$cachePool = new Definition();
5159
$cachePool->addTag('cache.pool', array(
5260
'provider' => 'foobar',
@@ -61,7 +69,7 @@ public function testArgsAreReplaced()
6169

6270
$this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
6371
$this->assertSame('foobar', (string) $cachePool->getArgument(0));
64-
$this->assertSame('kRFqMp5odS', $cachePool->getArgument(1));
72+
$this->assertSame('KO3xHaFEZU', $cachePool->getArgument(1));
6573
$this->assertSame(3, $cachePool->getArgument(2));
6674
}
6775

@@ -72,6 +80,10 @@ public function testArgsAreReplaced()
7280
public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
7381
{
7482
$container = new ContainerBuilder();
83+
$container->setParameter('kernel.debug', false);
84+
$container->setParameter('kernel.name', 'app');
85+
$container->setParameter('kernel.environment', 'prod');
86+
$container->setParameter('kernel.root_dir', 'foo');
7587
$adapter = new Definition();
7688
$adapter->setAbstract(true);
7789
$adapter->addTag('cache.pool');

0 commit comments

Comments
 (0)