[FrameworkBundle] Dont store cache misses on warmup

This commit is contained in:
Nyholm 2021-03-30 23:51:46 +02:00 committed by Nicolas Grekas
parent e16be83f91
commit 27a22b34af
3 changed files with 42 additions and 1 deletions

View File

@ -16,6 +16,7 @@ use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\DoctrineProvider;
/**
@ -76,6 +77,14 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
return true;
}
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
{
// make sure we don't cache null values
$values = array_filter($values, function ($val) { return null !== $val; });
parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values);
}
private function readAllComponents(Reader $reader, string $class)
{
$reflectionClass = new \ReflectionClass($class);

View File

@ -80,7 +80,9 @@ class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
{
// make sure we don't cache null values
parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values));
$values = array_filter($values, function ($val) { return null !== $val; });
parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values);
}
/**

View File

@ -8,6 +8,7 @@ use Doctrine\Common\Annotations\Reader;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\DoctrineProvider;
@ -120,6 +121,35 @@ class AnnotationsCacheWarmerTest extends TestCase
spl_autoload_unregister($classLoader);
}
public function testWarmupRemoveCacheMisses()
{
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
$warmer = $this->getMockBuilder(AnnotationsCacheWarmer::class)
->setConstructorArgs([new AnnotationReader(), $cacheFile])
->setMethods(['doWarmUp'])
->getMock();
$warmer->method('doWarmUp')->willReturnCallback(function ($cacheDir, ArrayAdapter $arrayAdapter) {
$arrayAdapter->getItem('foo_miss');
$item = $arrayAdapter->getItem('bar_hit');
$item->set('data');
$arrayAdapter->save($item);
$item = $arrayAdapter->getItem('baz_hit_null');
$item->set(null);
$arrayAdapter->save($item);
return true;
});
$warmer->warmUp($this->cacheDir);
$data = include $cacheFile;
$this->assertCount(1, $data[0]);
$this->assertTrue(isset($data[0]['bar_hit']));
}
/**
* @return MockObject|Reader
*/