Skip to content

[FrameworkBundle] Dont store cache misses on warmup #40645

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
Apr 1, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;

/**
Expand Down Expand Up @@ -76,6 +77,14 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -120,6 +121,35 @@ public function testClassAutoloadExceptionWithUnrelatedException()
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
*/
Expand Down