Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2f4465

Browse filesBrowse files
committed
[HttpKernel] Add CachePoolClearer for per-pool cache clearing
1 parent 4033b60 commit f2f4465
Copy full SHA for f2f4465

File tree

3 files changed

+74
-10
lines changed
Filter options

3 files changed

+74
-10
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Command/CachePoolClearCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/CachePoolClearCommand.php
+12-10Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14-
use Psr\Cache\CacheItemPoolInterface;
1514
use Symfony\Component\Console\Input\InputInterface;
1615
use Symfony\Component\Console\Input\InputArgument;
1716
use Symfony\Component\Console\Output\OutputInterface;
@@ -55,16 +54,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
5554
$clearers = array();
5655
$container = $this->getContainer();
5756
$cacheDir = $container->getParameter('kernel.cache_dir');
57+
$poolClearer = $container->get('cache.pool_clearer');
5858

5959
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;
60+
if ($poolClearer->hasPool($id)) {
61+
$pools[] = $id;
6662
} else {
67-
throw new \InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id));
63+
$clearer = $container->get($id);
64+
65+
if (!$clearer instanceof Psr6CacheClearer) {
66+
throw new \InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id));
67+
}
68+
69+
$clearers[$id] = $clearer;
6870
}
6971
}
7072

@@ -73,9 +75,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
7375
$clearer->clear($cacheDir);
7476
}
7577

76-
foreach ($pools as $id => $pool) {
78+
foreach ($pools as $id) {
7779
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));
78-
$pool->clear();
80+
$poolClearer->clearPool($id);
7981
}
8082

8183
$io->success('Cache was successfully cleared.');

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
21+
use Symfony\Component\HttpKernel\CacheClearer\CachePoolClearer;
2122

2223
/**
2324
* @author Nicolas Grekas <p@tchwork.com>
@@ -42,6 +43,10 @@ public function process(ContainerBuilder $container)
4243
'namespace',
4344
'default_lifetime',
4445
);
46+
47+
$poolClearer = new Definition(CachePoolClearer::class);
48+
$container->setDefinition('cache.pool_clearer', $poolClearer);
49+
4550
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
4651
$adapter = $pool = $container->getDefinition($id);
4752
if ($pool->isAbstract()) {
@@ -83,6 +88,8 @@ public function process(ContainerBuilder $container)
8388
if (null !== $clearer) {
8489
$pool->addTag('cache.pool', array('clearer' => $clearer));
8590
}
91+
92+
$poolClearer->addMethodCall('addPool', array($id, new Reference($id)));
8693
}
8794
}
8895

+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Component\HttpKernel\CacheClearer;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
16+
/**
17+
* PSR-6 per-pool cache clearer.
18+
*
19+
* @author Robin Chalas <robin.chalas@gmail.com>
20+
*
21+
* @internal
22+
*/
23+
final class CachePoolClearer implements CacheClearerInterface
24+
{
25+
private $pools = array();
26+
27+
public function addPool($id, CacheItemPoolInterface $pool)
28+
{
29+
$this->pools[$id] = $pool;
30+
}
31+
32+
public function hasPool($id)
33+
{
34+
return isset($this->pools[$id]);
35+
}
36+
37+
public function clearPool($id)
38+
{
39+
if (!$this->hasPool($id)) {
40+
throw new \LogicException(sprintf('Cache pool "%s" doesn\'t exist', $id));
41+
}
42+
43+
$this->pools[$id]->clear();
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function clear($cacheDir)
50+
{
51+
foreach ($this->pools as $pool) {
52+
$pool->clear();
53+
}
54+
}
55+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.