Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Don't populate fallback cache on warmup #28331

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 3, 2018
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 @@ -11,11 +11,9 @@

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

/**
Expand All @@ -24,19 +22,13 @@
abstract class AbstractPhpFileCacheWarmer implements CacheWarmerInterface
{
private $phpArrayFile;
private $fallbackPool;

/**
* @param string $phpArrayFile The PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
* @param string $phpArrayFile The PHP file where metadata are cached
*/
public function __construct(string $phpArrayFile, CacheItemPoolInterface $fallbackPool)
public function __construct(string $phpArrayFile)
{
$this->phpArrayFile = $phpArrayFile;
if (!$fallbackPool instanceof AdapterInterface) {
$fallbackPool = new ProxyAdapter($fallbackPool);
}
$this->fallbackPool = $fallbackPool;
}

/**
Expand Down Expand Up @@ -68,13 +60,7 @@ public function warmUp($cacheDir)
// so here we un-serialize the values first
$values = array_map(function ($val) { return null !== $val ? unserialize($val) : null; }, $arrayAdapter->getValues());

$this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool), $values);

foreach ($values as $k => $v) {
$item = $this->fallbackPool->getItem($k);
$this->fallbackPool->saveDeferred($item->set($v));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main call that I propose to remove in this PR (the rest is just collateral)

}
$this->fallbackPool->commit();
$this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
}

protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
private $debug;

/**
* @param Reader $annotationReader
* @param string $phpArrayFile The PHP file where annotations are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached
* @param string $phpArrayFile The PHP file where annotations are cached
* @param string $excludeRegexp
* @param bool $debug
*/
public function __construct(Reader $annotationReader, string $phpArrayFile, CacheItemPoolInterface $fallbackPool, string $excludeRegexp = null, bool $debug = false)
public function __construct(Reader $annotationReader, string $phpArrayFile, $excludeRegexp = null, $debug = false)
{
parent::__construct($phpArrayFile, $fallbackPool);
if ($excludeRegexp instanceof CacheItemPoolInterface) {
@trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.', __METHOD__), E_USER_DEPRECATED);
$excludeRegexp = $debug;
$debug = 4 < \func_num_args() && \func_get_arg(4);
}
parent::__construct($phpArrayFile);
$this->annotationReader = $annotationReader;
$this->excludeRegexp = $excludeRegexp;
$this->debug = $debug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
private $loaders;

/**
* @param LoaderInterface[] $loaders The serializer metadata loaders
* @param string $phpArrayFile The PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
* @param LoaderInterface[] $loaders The serializer metadata loaders
* @param string $phpArrayFile The PHP file where metadata are cached
*/
public function __construct(array $loaders, string $phpArrayFile, CacheItemPoolInterface $fallbackPool)
public function __construct(array $loaders, string $phpArrayFile)
{
parent::__construct($phpArrayFile, $fallbackPool);
if (2 < \func_num_args() && \func_get_arg(2) instanceof CacheItemPoolInterface) {
@trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.', __METHOD__), E_USER_DEPRECATED);
}
parent::__construct($phpArrayFile);
$this->loaders = $loaders;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
private $validatorBuilder;

/**
* @param ValidatorBuilderInterface $validatorBuilder
* @param string $phpArrayFile The PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
* @param string $phpArrayFile The PHP file where metadata are cached
*/
public function __construct(ValidatorBuilderInterface $validatorBuilder, string $phpArrayFile, CacheItemPoolInterface $fallbackPool)
public function __construct(ValidatorBuilderInterface $validatorBuilder, string $phpArrayFile)
{
parent::__construct($phpArrayFile, $fallbackPool);
if (2 < \func_num_args() && \func_get_arg(2) instanceof CacheItemPoolInterface) {
@trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.', __METHOD__), E_USER_DEPRECATED);
}
parent::__construct($phpArrayFile);
$this->validatorBuilder = $validatorBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<service id="annotations.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer">
<argument type="service" id="annotations.reader" />
<argument>%kernel.cache_dir%/annotations.php</argument>
<argument type="service" id="cache.annotations" />
<argument>#^Symfony\\(?:Component\\HttpKernel\\|Bundle\\FrameworkBundle\\Controller\\(?!AbstractController$|Controller$))#</argument>
<argument>%kernel.debug%</argument>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
<service id="serializer.mapping.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer">
<argument type="collection" /><!-- Loaders injected by the extension -->
<argument>%serializer.mapping.cache.file%</argument>
<argument type="service" id="cache.serializer" />
<tag name="kernel.cache_warmer" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<service id="validator.mapping.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\ValidatorCacheWarmer">
<argument type="service" id="validator.builder" />
<argument>%validator.mapping.cache.file%</argument>
<argument type="service" id="cache.validator" />
<tag name="kernel.cache_warmer" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\Annotations\Reader;
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 @@ -37,13 +36,7 @@ public function testAnnotationsCacheWarmerWithDebugDisabled()
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export(array(__CLASS__), true)));
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
$reader = new AnnotationReader();
$fallbackPool = new ArrayAdapter();
$warmer = new AnnotationsCacheWarmer(
$reader,
$cacheFile,
$fallbackPool,
null
);
$warmer = new AnnotationsCacheWarmer($reader, $cacheFile);
$warmer->warmUp($this->cacheDir);
$this->assertFileExists($cacheFile);

Expand All @@ -63,14 +56,7 @@ public function testAnnotationsCacheWarmerWithDebugEnabled()
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export(array(__CLASS__), true)));
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
$reader = new AnnotationReader();
$fallbackPool = new ArrayAdapter();
$warmer = new AnnotationsCacheWarmer(
$reader,
$cacheFile,
$fallbackPool,
null,
true
);
$warmer = new AnnotationsCacheWarmer($reader, $cacheFile, null, true);
$warmer->warmUp($this->cacheDir);
$this->assertFileExists($cacheFile);
// Assert cache is valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer;
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\Serializer\Mapping\Factory\CacheClassMetadataFactory;
Expand All @@ -36,9 +35,7 @@ public function testWarmUp()
$file = sys_get_temp_dir().'/cache-serializer.php';
@unlink($file);

$fallbackPool = new ArrayAdapter();

$warmer = new SerializerCacheWarmer($loaders, $file, $fallbackPool);
$warmer = new SerializerCacheWarmer($loaders, $file);
$warmer->warmUp(\dirname($file));

$this->assertFileExists($file);
Expand All @@ -47,13 +44,6 @@ public function testWarmUp()

$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());

$values = $fallbackPool->getValues();

$this->assertInternalType('array', $values);
$this->assertCount(2, $values);
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person', $values);
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author', $values);
}

public function testWarmUpWithoutLoader()
Expand All @@ -65,16 +55,9 @@ public function testWarmUpWithoutLoader()
$file = sys_get_temp_dir().'/cache-serializer-without-loader.php';
@unlink($file);

$fallbackPool = new ArrayAdapter();

$warmer = new SerializerCacheWarmer(array(), $file, $fallbackPool);
$warmer = new SerializerCacheWarmer(array(), $file);
$warmer->warmUp(\dirname($file));

$this->assertFileExists($file);

$values = $fallbackPool->getValues();

$this->assertInternalType('array', $values);
$this->assertCount(0, $values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Bundle\FrameworkBundle\CacheWarmer\ValidatorCacheWarmer;
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\Validator\Mapping\ClassMetadata;
Expand All @@ -32,9 +31,7 @@ public function testWarmUp()
$file = sys_get_temp_dir().'/cache-validator.php';
@unlink($file);

$fallbackPool = new ArrayAdapter();

$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool);
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
$warmer->warmUp(\dirname($file));

$this->assertFileExists($file);
Expand All @@ -43,13 +40,6 @@ public function testWarmUp()

$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());

$values = $fallbackPool->getValues();

$this->assertInternalType('array', $values);
$this->assertCount(2, $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person', $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author', $values);
}

public function testWarmUpWithAnnotations()
Expand All @@ -61,9 +51,7 @@ public function testWarmUpWithAnnotations()
$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
@unlink($file);

$fallbackPool = new ArrayAdapter();

$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool);
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
$warmer->warmUp(\dirname($file));

$this->assertFileExists($file);
Expand All @@ -74,13 +62,6 @@ public function testWarmUpWithAnnotations()
$this->assertTrue($item->isHit());

$this->assertInstanceOf(ClassMetadata::class, $item->get());

$values = $fallbackPool->getValues();

$this->assertInternalType('array', $values);
$this->assertCount(2, $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.SubCategory', $values);
}

public function testWarmUpWithoutLoader()
Expand All @@ -90,16 +71,9 @@ public function testWarmUpWithoutLoader()
$file = sys_get_temp_dir().'/cache-validator-without-loaders.php';
@unlink($file);

$fallbackPool = new ArrayAdapter();

$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool);
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
$warmer->warmUp(\dirname($file));

$this->assertFileExists($file);

$values = $fallbackPool->getValues();

$this->assertInternalType('array', $values);
$this->assertCount(0, $values);
}
}