10000 [FrameworkBundle] Dont store cache misses on warmup · symfony/symfony@3cec54f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cec54f

Browse files
committed
[FrameworkBundle] Dont store cache misses on warmup
1 parent e16be83 commit 3cec54f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ public function warmUp($cacheDir)
5353
spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
5454
}
5555

56+
// Remove cache misses (null values)
57+
$values = array_filter($arrayAdapter->getValues(), function ($val) { return null !== $val; });
58+
5659
// the ArrayAdapter stores the values serialized
5760
// to avoid mutation of the data after it was written to the cache
5861
// so here we un-serialize the values first
59-
$values = array_map(function ($val) { return null !== $val ? unserialize($val) : null; }, $arrayAdapter->getValues());
62+
$values = array_map(function ($val) { return null !== $val ? unserialize($val) : null; }, $values);
6063

6164
$this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
6265
}

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\MockObject\MockObject;
99
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
1010
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
11+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1112
use Symfony\Component\Cache\Adapter\NullAdapter;
1213
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
1314
use Symfony\Component\Cache\DoctrineProvider;
@@ -120,6 +121,36 @@ public function testClassAutoloadExceptionWithUnrelatedException()
120121
spl_autoload_unregister($classLoader);
121122
}
122123

124+
public function testWarmupRemoveCacheMisses()
125+
{
126+
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
127+
$warmer = $this->getMockBuilder(AnnotationsCacheWarmer::class)
128+
->setConstructorArgs([new AnnotationReader(), $cacheFile])
129+
->onlyMethods(['doWarmUp'])
130+
->getMock();
131+
132+
$warmer->method('doWarmUp')->willReturnCallback(function ($cacheDir, ArrayAdapter $arrayAdapter) {
133+
$arrayAdapter->getItem('foo_miss');
134+
135+
$item = $arrayAdapter->getItem('bar_hit');
136+
$item->set('data');
137+
$arrayAdapter->save($item);
138+
139+
$item = $arrayAdapter->getItem('baz_hit_null');
140+
$item->set(null);
141+
$arrayAdapter->save($item);
142+
143+
return true;
144+
});
145+
146+
$warmer->warmUp($this->cacheDir);
147+
$data = include $cacheFile;
148+
149+
$this->assertCount(2, $data[0]);
150+
$this->assertTrue(isset($data[0]['bar_hit']));
151+
$this->assertTrue(isset($data[0]['baz_hit_null']));
152+
}
153+
123154
/**
124155
* @return MockObject|Reader
125156
*/

0 commit comments

Comments
 (0)
0