Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache warmer will come in the next commits. To warm up the cache on a production server, you can use the cache:warmup command: ./app/console_prod cache:warmup
- Loading branch information
Showing
10 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Warmup the cache. | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class CacheWarmupCommand extends Command | ||
{ | ||
/** | ||
* @see Command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('cache:warmup') | ||
->setDescription('Warms up an empty cache') | ||
->setHelp(<<<EOF | ||
The <info>cache:warmup</info> command warms up the cache. | ||
Before running this command, the cache must be empty. | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$output->writeln('Warming up the cache'); | ||
|
||
$warmer = $this->container->get('cache_warmer'); | ||
$warmer->enableOptionalWarmers(); | ||
$warmer->warmUp($this->container->getParameter('kernel.cache_dir')); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheWarmerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* Registers the cache warmers. | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class AddCacheWarmerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('cache_warmer')) { | ||
return; | ||
} | ||
|
||
$warmers = array(); | ||
foreach ($container->findTaggedServiceIds('kernel.cache_warmer') as $id => $attributes) { | ||
$warmers[] = new Reference($id); | ||
} | ||
|
||
$container->getDefinition('cache_warmer')->setArgument(0, $warmers); | ||
|
||
if ('full' === $container->getParameter('kernel.cache_warmup')) { | ||
$container->getDefinition('cache_warmer')->addMethodCall('enableOptionalWarmers', array()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\CacheWarmer; | ||
|
||
/** | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.org> | ||
*/ | ||
abstract class CacheWarmer implements CacheWarmerInterface | ||
{ | ||
protected function writeCacheFile($file, $content) | ||
{ | ||
$tmpFile = tempnam(dirname($file), basename($file)); | ||
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) { | ||
chmod($file, 0644); | ||
|
||
return; | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\CacheWarmer; | ||
|
||
/** | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.org> | ||
*/ | ||
class CacheWarmerAggregate implements CacheWarmerInterface | ||
{ | ||
protected $warmers; | ||
protected $optionalsEnabled; | ||
|
||
public function __construct(array $warmers = array()) | ||
{ | ||
$this->setWarmers($warmers); | ||
$this->optionalsEnabled = false; | ||
} | ||
|
||
public function enableOptionalWarmers() | ||
{ | ||
$this->optionalsEnabled = true; | ||
} | ||
|
||
/** | ||
* Warms up the cache. | ||
* | ||
* @param string $cacheDir The cache directory | ||
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
foreach ($this->warmers as $warmer) { | ||
if (!$this->optionalsEnabled && $warmer->isOptional()) { | ||
continue; | ||
} | ||
|
||
$warmer->warmUp($cacheDir); | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether this warmer is optional or not. | ||
* | ||
* @return Boolean always true | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sstok
Contributor
|
||
*/ | ||
public function isOptional() | ||
{ | ||
return false; | ||
} | ||
|
||
public function setWarmers(array $warmers) | ||
{ | ||
$this->warmers = array(); | ||
foreach ($warmers as $warmer) { | ||
$this->add($warmer); | ||
} | ||
} | ||
|
||
public function add(CacheWarmerInterface $warmer) | ||
{ | ||
$this->warmers[] = $warmer; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\CacheWarmer; | ||
|
||
/** | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.org> | ||
*/ | ||
interface CacheWarmerInterface | ||
{ | ||
/** | ||
* Warms up the cache. | ||
* | ||
* @param string $cacheDir The cache directory | ||
*/ | ||
function warmUp($cacheDir); | ||
|
||
/** | ||
* Checks whether this warmer is optional or not. | ||
* | ||
* Optional warmers can be ignored on certain conditions. | ||
* | ||
* A warmer should return true if the cache can be | ||
* generated incrementally and on-demand. | ||
* | ||
* @return Boolean true if the warmer is optional, false otherwise | ||
*/ | ||
function isOptional(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I think there is a mistake here, the method returns directly false, not true