8000 [Cache] Add ResettableInterface to allow resetting any pool's local s… · symfony/symfony@db3837d · GitHub
[go: up one dir, main page]

Skip to content

Commit db3837d

Browse files
[Cache] Add ResettableInterface to allow resetting any pool's local state
1 parent e3fa71c commit db3837d

33 files changed

+315
-70
lines changed

src/Symfony/Bundle/FrameworkBundle/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) {

src/Symfony/Bundle/FrameworkBundle/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;
@@ -336,6 +337,8 @@ public function load(array $configs, ContainerBuilder $container)
336337
->addTag('kernel.cache_warmer');
337338
$container->registerForAutoconfiguration(EventSubscriberInterface::class)
338339
->addTag('kernel.event_subscriber');
340+
$container->registerForAutoconfiguration(ResettableInterface::class)
341+
->addTag('kernel.reset', array('method' => 'reset'));
339342
$container->registerForAutoconfiguration(PropertyListExtractorInterface::class)
340343
->addTag('property_info.list_extractor');
341344
$container->registerForAutoconfiguration(PropertyTypeExtractorInterface::class)

src/Symfony/Bundle/FrameworkBundle/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 -->

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
use Psr\Log\NullLogger;
1818
use Symfony\Component\Cache\CacheItem;
1919
use Symfony\Component\Cache\Exception\InvalidArgumentException;
20+
use Symfony\Component\Cache\ResettableInterface;
2021
use Symfony\Component\Cache\Traits\AbstractTrait;
2122

2223
/**
2324
* @author Nicolas Grekas <p@tchwork.com>
2425
*/
25-
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
26+
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
2627
{
2728
use AbstractTrait;
2829

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Log\LoggerAwareInterface;
1616
use Symfony\Component\Cache\CacheItem;
17+
use Symfony\Component\Cache\ResettableInterface;
1718
use Symfony\Component\Cache\Traits\ArrayTrait;
1819

1920
/**
2021
* @author Nicolas Grekas <p@tchwork.com>
2122
*/
22-
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
23+
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
2324
{
2425
use ArrayTrait;
2526

src/Symfony/Component/Cache/Adapter/ChainAdapter.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Cache\CacheItem;
1717
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1818
use Symfony\Component\Cache\PruneableInterface;
19+
use Symfony\Component\Cache\ResettableInterface;
1920

2021
/**
2122
* Chains several adapters together.
@@ -25,7 +26,7 @@
2526
*
2627
* @author Kévin Dunglas <dunglas@gmail.com>
2728
*/
28-
class ChainAdapter implements AdapterInterface, PruneableInterface
29+
class ChainAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
2930
{
3031
private $adapters = array();
3132
private $adapterCount;
@@ -248,4 +249,16 @@ public function prune()
248249

249250
return $pruned;
250251
}
252+
253+
/**
254+
* {@inheritdoc}
255+
*/
256+
public function reset()
257+
{
258+
foreach ($this->adapters as $adapter) {
259+
if ($adapter instanceof ResettableInterface) {
260+
$adapter->reset();
261+
}
262+
}
263+
}
251264
}

src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Psr\Cache\CacheItemPoolInterface;
1616
use Symfony\Component\Cache\CacheItem;
1717
use Symfony\Component\Cache\Exception\InvalidArgumentException;
18+
use Symfony\Component\Cache\PruneableInterface;
19+
use Symfony\Component\Cache\ResettableInterface;
1820
use Symfony\Component\Cache\Traits\PhpArrayTrait;
1921

2022
/**
@@ -24,7 +26,7 @@
2426
* @author Titouan Galopin <galopintitouan@gmail.com>
2527
* @author Nicolas Grekas <p@tchwork.com>
2628
*/
27-
class PhpArrayAdapter implements AdapterInterface
29+
class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
2830
{
2931
use PhpArrayTrait;
3032

@@ -37,7 +39,7 @@ class PhpArrayAdapter implements AdapterInterface
3739
public function __construct($file, AdapterInterface $fallbackPool)
3840
{
3941
$this->file = $file;
40-
$this->fallbackPool = $fallbackPool;
42+
$this->pool = $fallbackPool;
4143
$this->zendDetectUnicode = ini_get('zend.detect_unicode');
4244
$this->createCacheItem = \Closure::bind(
4345
function ($key, $value, $isHit) {
@@ -89,7 +91,7 @@ public function getItem($key)
8991
$this->initialize();
9092
}
9193
if (!isset($this->values[$key])) {
92-
return $this->fallbackPool->getItem($key);
94+
return $this->pool->getItem($key);
9395
}
9496

9597
$value = $this->values[$key];
@@ -144,7 +146,7 @@ public function hasItem($key)
144146
$this->initialize();
145147
}
146148

147-
return isset($this->values[$key]) || $this->fallbackPool->hasItem($key);
149+
return isset($this->values[$key]) || $this->pool->hasItem($key);
148150
}
149151

150152
/**
@@ -159,7 +161,7 @@ public function deleteItem($key)
159161
$this->initialize();
160162
}
161163

162-
return !isset($this->values[$key]) && $this->fallbackPool->deleteItem($key);
164+
return !isset($this->values[$key]) && $this->pool->deleteItem($key);
163165
}
164166

165167
/**
@@ -186,7 +188,7 @@ public function deleteItems(array $keys)
186188
}
187189

188190
if ($fallbackKeys) {
189-
$deleted = $this->fallbackPool->deleteItems($fallbackKeys) && $deleted;
191+
$deleted = $this->pool->deleteItems($fallbackKeys) && $deleted;
190192
}
191193

192194
return $deleted;
@@ -201,7 +203,7 @@ public function save(CacheItemInterface $item)
201203
$this->initialize();
202204
}
203205

204-
return !isset($this->values[$item->getKey()]) && $this->fallbackPool->save($item);
206+
return !isset($this->values[$item->getKey()]) && $this->pool->save($item);
205207
}
206208

207209
/**
@@ -213,15 +215,15 @@ public function saveDeferred(CacheItemInterface $item)
213215
$this->initialize();
214216
}
215217

216-
return !isset($this->values[$item->getKey()]) && $this->fallbackPool->saveDeferred($item);
218+
return !isset($this->values[$item->getKey()]) && $this->pool->saveDeferred($item);
217219
}
218220

219221
/**
220222
* {@inheritdoc}
221223
*/
222224
public function commit()
223225
{
224-
return $this->fallbackPool->commit();
226+
return $this->pool->commit();
225227
}
226228

227229
/**
@@ -259,7 +261,7 @@ private function generateItems(array $keys)
259261
}
260262

261263
if ($fallbackKeys) {
262-
foreach ($this->fallbackPool->getItems($fallbackKeys) as $key => $item) {
264+
foreach ($this->pool->getItems($fallbackKeys) as $key => $item) {
263265
yield $key => $item;
264266
}
265267
}

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
1616
use Symfony\Component\Cache\CacheItem;
17+
use Symfony\Component\Cache\PruneableInterface;
18+
use Symfony\Component\Cache\ResettableInterface;
19+
use Symfony\Component\Cache\Traits\ProxyTrait;
1720

1821
/**
1922
* @author Nicolas Grekas <p@tchwork.com>
2023
*/
21-
class ProxyAdapter implements AdapterInterface
24+
class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
2225
{
23-
private $pool;
26+
use ProxyTrait;
27+
2428
private $namespace;
2529
private $namespaceLen;
2630
private $createCacheItem;

src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
namespace Symfony\Component\Cache\Adapter;
1313

1414
use Psr\SimpleCache\CacheInterface;
15+
use Symfony\Component\Cache\PruneableInterface;
16+
use Symfony\Component\Cache\ResettableInterface;
17+
use Symfony\Component\Cache\Traits\ProxyTrait;
1518

1619
/**
1720
* @author Nicolas Grekas <p@tchwork.com>
1821
*/
19-
class SimpleCacheAdapter extends AbstractAdapter
22+
class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
2023
{
21-
private $pool;
24+
use ProxyTrait;
25+
2226
private $miss;
2327

2428
public function __construct(CacheInterface $pool, $namespace = '', $defaultLifetime = 0)

0 commit comments

Comments
 (0)
0