Skip to content

Commit 487386a

Browse files
committed
feature #19891 [FrameworkBundle] Add cache:pool:clear command (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [FrameworkBundle] Add cache:pool:clear command | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | License | MIT Useful when deploying apps to get better control of cache pool clearing. Commits ------- 37c5b18 [FrameworkBundle] Add cache:pool:clear command
2 parents 438d6ac + 0a40237 commit 487386a

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

Command/CachePoolClearCommand.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
19+
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
20+
21+
/**
22+
* Clear cache pools.
23+
*
24+
* @author Nicolas Grekas <p@tchwork.com>
25+
*/
26+
final class CachePoolClearCommand extends ContainerAwareCommand
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function configure()
32+
{
33+
$this
34+
->setName('cache:pool:clear')
35+
->setDefinition(array(
36+
new InputArgument('pools', InputArgument::IS_ARRAY, 'A list of cache pools or cache pool clearers'),
37+
))
38+
->setDescription('Clears cache pools')
39+
->setHelp(<<<'EOF'
40+
The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
41+
42+
%command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>]
43+
EOF
44+
)
45+
;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function execute(InputInterface $input, OutputInterface $output)
52+
{
53+
$io = new SymfonyStyle($input, $output);
54+
$pools = array();
55+
$clearers = array();
56+
$container = $this->getContainer();
57+
$cacheDir = $container->getParameter('kernel.cache_dir');
58+
59+
foreach ($input->getArgument('pools') as $id) {
60+
$pool = $container->get($id);
61+
62+
if ($pool instanceof CacheItemPoolInterface) {
63+
$pools[$id] = $pool;
64+
} elseif ($pool instanceof Psr6CacheClearer) {
65+
$clearers[$id] = $pool;
66+
} else {
67+
throw new \InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id));
68+
}
69+
}
70+
71+
foreach ($clearers as $id => $clearer) {
72+
$io->comment(sprintf('Calling cache clearer: <info>%s</info>', $id));
73+
$clearer->clear($cacheDir);
74+
}
75+
76+
foreach ($pools as $id => $pool) {
77+
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));
78+
$pool->clear();
79+
}
80+
81+
$io->success('Cache was successfully cleared.');
82+
}
83+
}

Resources/config/cache.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<services>
88

99
<service id="cache.app" parent="cache.adapter.filesystem">
10-
<tag name="cache.pool" />
10+
<tag name="cache.pool" clearer="cache.app_clearer" />
1111
</service>
1212

1313
<service id="cache.system" parent="cache.adapter.system">
@@ -92,5 +92,7 @@
9292
<tag name="kernel.cache_clearer" />
9393
</service>
9494

95+
<service id="cache.app_clearer" alias="cache.default_clearer" />
96+
9597
</services>
9698
</container>

0 commit comments

Comments
 (0)