Skip to content

Commit 8de2041

Browse files
feature #24226 [Cache] Add ResettableInterface to allow resetting any pool's local state (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [Cache] Add ResettableInterface to allow resetting any pool's local state | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - To allow pools to leverage #24155 so that they can be used in multi-request loops. Commits ------- 14c91f2 [Cache] Add ResettableInterface to allow resetting any pool's local state
2 parents ef54c58 + 8143468 commit 8de2041

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function process(ContainerBuilder $container)
4343
'provider',
4444
'namespace',
4545
'default_lifetime',
46+
'reset',
4647
);
4748
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
4849
$adapter = $pool = $container->getDefinition($id);
@@ -73,13 +74,19 @@ public function process(ContainerBuilder $container)
7374
}
7475
$i = 0;
7576
foreach ($attributes as $attr) {
76-
if (isset($tags[0][$attr]) && ('namespace' !== $attr || ArrayAdapter::class !== $adapter->getClass())) {
77+
if (!isset($tags[0][$attr])) {
78+
// no-op
79+
} elseif ('reset' === $attr) {
80+
if ($tags[0][$attr]) {
81+
$pool->addTag('kernel.reset', array('method' => $tags[0][$attr]));
82+
}
83+
} elseif ('namespace' !== $attr || ArrayAdapter::class !== $adapter->getClass()) {
7784
$pool->replaceArgument($i++, $tags[0][$attr]);
7885
}
7986
unset($tags[0][$attr]);
8087
}
8188
if (!empty($tags[0])) {
82-
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0]))));
89+
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace", "default_lifetime" and "reset", found "%s".', $id, implode('", "', array_keys($tags[0]))));
8390
}
8491

8592
if (null !== $clearer) {

DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
2626
use Symfony\Component\Cache\Adapter\AdapterInterface;
2727
use Symfony\Component\Cache\Adapter\ArrayAdapter;
28+
use Symfony\Component\Cache\ResettableInterface;
2829
use Symfony\Component\Config\FileLocator;
2930
use Symfony\Component\Config\Loader\LoaderInterface;
3031
use Symfony\Component\Config\Resource\DirectoryResource;
@@ -339,6 +340,8 @@ public function load(array $configs, ContainerBuilder $container)
339340
->addTag('kernel.cache_warmer');
340341
$container->registerForAutoconfiguration(EventSubscriberInterface::class)
341342
->addTag('kernel.event_subscriber');
343+
$container->registerForAutoconfiguration(ResettableInterface::class)
344+
->addTag('kernel.reset', array('method' => 'reset'));
342345
$container->registerForAutoconfiguration(PropertyListExtractorInterface::class)
343346
->addTag('property_info.list_extractor');
344347
$container->registerForAutoconfiguration(PropertyTypeExtractorInterface::class)

Resources/config/cache.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<defaults public="false" />
99

1010
<service id="cache.app" parent="cache.adapter.filesystem" public="true">
11-
<tag name="cache.pool" clearer="cache.app_clearer" />
11+
<tag name="cache.pool" clearer="cache.app_clearer" reset="reset" />
1212
</service>
1313

1414
<service id="cache.system" parent="cache.adapter.system" public="true">
@@ -90,7 +90,7 @@
9090
</service>
9191

9292
<service id="cache.adapter.memcached" class="Symfony\Component\Cache\Adapter\MemcachedAdapter" abstract="true">
93-
<tag name="cache.pool" provider="cache.default_memcached_provider" clearer="cache.default_clearer" />
93+
<tag name="cache.pool" provider="cache.default_memcached_provider" clearer="cache.default_clearer" reset="reset" />
9494
<tag name="monolog.logger" channel="cache" />
9595
<argument /> <!-- Memcached connection service -->
9696
<argument /> <!-- namespace -->

0 commit comments

Comments
 (0)