-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Add (filesystem|phpfiles) cache (adapter|simple) prune method and prune command #23451
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
nicolas-grekas
merged 1 commit into
symfony:3.4
from
robfrawley:feature-filesystem-cache-prune
Jul 22, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
70 changes: 70 additions & 0 deletions
70
src/Symfony/Bundle/FrameworkBundle/Command/CachePoolPruneCommand.php
This file contains hidden or 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,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.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\Cache\PruneableInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* Cache pool pruner command. | ||
* | ||
* @author Rob Frawley 2nd <rmf@src.run> | ||
*/ | ||
final class CachePoolPruneCommand extends Command | ||
{ | ||
private $pools; | ||
|
||
/** | ||
* @param iterable|PruneableInterface[] $pools | ||
*/ | ||
public function __construct($pools) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->pools = $pools; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('cache:pool:prune') | ||
->setDescription('Prune cache pools') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command deletes all expired items from all pruneable pools. | ||
|
||
%command.full_name% | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
foreach ($this->pools as $name => $pool) { | ||
$io->comment(sprintf('Pruning cache pool: <info>%s</info>', $name)); | ||
$pool->prune(); | ||
} | ||
|
||
$io->success('Successfully pruned cache pool(s).'); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPrunerPass.php
This file contains hidden or 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,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\Cache\PruneableInterface; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Rob Frawley 2nd <rmf@src.run> | ||
*/ | ||
class CachePoolPrunerPass implements CompilerPassInterface | ||
{ | ||
private $cacheCommandServiceId; | ||
private $cachePoolTag; | ||
|
||
public function __construct($cacheCommandServiceId = 'cache.command.pool_pruner', $cachePoolTag = 'cache.pool') | ||
{ | ||
$this->cacheCommandServiceId = $cacheCommandServiceId; | ||
$this->cachePoolTag = $cachePoolTag; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition($this->cacheCommandServiceId)) { | ||
return; | ||
} | ||
|
||
$services = array(); | ||
|
||
foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $tags) { | ||
$class = $container->getParameterBag()->resolveValue($container->getDefinition($id)->getClass()); | ||
|
||
if (!$reflection = $container->getReflectionClass($class)) { | ||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); | ||
} | ||
|
||
if ($reflection->implementsInterface(PruneableInterface::class)) { | ||
$services[$id] = new Reference($id); | ||
} | ||
} | ||
|
||
$container->getDefinition($this->cacheCommandServiceId)->replaceArgument(0, new IteratorArgument($services)); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
112 changes: 112 additions & 0 deletions
112
src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php
This file contains hidden or 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,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Command\CachePoolPruneCommand; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Symfony\Component\Cache\PruneableInterface; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
class CachePruneCommandTest extends TestCase | ||
{ | ||
public function testCommandWithPools() | ||
{ | ||
$tester = $this->getCommandTester($this->getKernel(), $this->getRewindableGenerator()); | ||
$tester->execute(array()); | ||
} | ||
|
||
public function testCommandWithNoPools() | ||
{ | ||
$tester = $this->getCommandTester($this->getKernel(), $this->getEmptyRewindableGenerator()); | ||
$tester->execute(array()); | ||
} | ||
|
||
/** | ||
* @return RewindableGenerator | ||
*/ | ||
private function getRewindableGenerator() | ||
{ | ||
return new RewindableGenerator(function () { | ||
yield 'foo_pool' => $this->getPruneableInterfaceMock(); | ||
yield 'bar_pool' => $this->getPruneableInterfaceMock(); | ||
}, 2); | ||
} | ||
|
||
/** | ||
* @return RewindableGenerator | ||
*/ | ||
private function getEmptyRewindableGenerator() | ||
{ | ||
return new RewindableGenerator(function () { | ||
return new \ArrayIterator(array()); | ||
}, 0); | ||
} | ||
|
||
/** | ||
* @return \PHPUnit_Framework_MockObject_MockObject|KernelInterface | ||
*/ | ||
private function getKernel() | ||
{ | ||
$container = $this | ||
->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface') | ||
->getMock(); | ||
|
||
$kernel = $this | ||
->getMockBuilder(KernelInterface::class) | ||
->getMock(); | ||
|
||
$kernel | ||
->expects($this->any()) | ||
->method('getContainer') | ||
->willReturn($container); | ||
|
||
$kernel | ||
->expects($this->once()) | ||
->method('getBundles') | ||
->willReturn(array()); | ||
|
||
return $kernel; | ||
} | ||
|
||
/** | ||
* @return \PHPUnit_Framework_MockObject_MockObject|PruneableInterface | ||
*/ | ||
private function getPruneableInterfaceMock() | ||
{ | ||
$pruneable = $this | ||
->getMockBuilder(PruneableInterface::class) | ||
->getMock(); | ||
|
||
$pruneable | ||
->expects($this->atLeastOnce()) | ||
->method('prune'); | ||
|
||
return $pruneable; | ||
} | ||
|
||
/** | ||
* @param KernelInterface $kernel | ||
* @param RewindableGenerator $generator | ||
* | ||
* @return CommandTester | ||
*/ | ||
private function getCommandTester(KernelInterface $kernel, RewindableGenerator $generator) | ||
{ | ||
$application = new Application($kernel); | ||
$application->add(new CachePoolPruneCommand($generator)); | ||
|
||
return new CommandTester($application->find('cache:pool:prune')); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...ony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPrunerPassTest.php
This file contains hidden or 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,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPrunerPass; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpFilesAdapter; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class CachePoolPrunerPassTest extends TestCase | ||
{ | ||
public function testCompilerPassReplacesCommandArgument() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('cache.command.pool_pruner')->addArgument(array()); | ||
$container->register('pool.foo', FilesystemAdapter::class)->addTag('cache.pool'); | ||
$container->register('pool.bar', PhpFilesAdapter::class)->addTag('cache.pool'); | ||
|
||
$pass = new CachePoolPrunerPass(); | ||
$pass->process($container); | ||
|
||
$expected = array( | ||
'pool.foo' => new Reference('pool.foo'), | ||
'pool.bar' => new Reference('pool.bar'), | ||
); | ||
$argument = $container->getDefinition('cache.command.pool_pruner')->getArgument(0); | ||
|
||
$this->assertInstanceOf(IteratorArgument::class, $argument); | ||
$this->assertEquals($expected, $argument->getValues()); | ||
} | ||
|
||
public function testCompilePassIsIgnoredIfCommandDoesNotExist() | ||
{ | ||
$container = $this | ||
->getMockBuilder(ContainerBuilder::class) | ||
->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds')) | ||
->getMock(); | ||
|
||
$container | ||
->expects($this->atLeastOnce()) | ||
->method('hasDefinition') | ||
->with('cache.command.pool_pruner') | ||
->will($this->returnValue(false)); | ||
|
||
$container | ||
->expects($this->never()) | ||
->method('getDefinition'); | ||
|
||
$container | ||
->expects($this->never()) | ||
->method('findTaggedServiceIds'); | ||
|
||
$pass = new CachePoolPrunerPass(); | ||
$pass->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage Class "Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\NotFound" used for service "pool.not-found" cannot be found. | ||
*/ | ||
public function testCompilerPassThrowsOnInvalidDefinitionClass() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('cache.command.pool_pruner')->addArgument(array()); | ||
$container->register('pool.not-found', NotFound::class)->addTag('cache.pool'); | ||
|
||
$pass = new CachePoolPrunerPass(); | ||
$pass->process($container); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure this is broken right now as
cache.command.pool_pruner
doesn't exist.