Skip to content

[Cache] Add ResettableInterface to allow resetting any pool's local state #24226

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
merged 1 commit into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Cache] Add ResettableInterface to allow resetting any pool's local s…
…tate
  • Loading branch information
nicolas-grekas committed Sep 25, 2017
commit 14c91f2bc98978c400eef484cdf7d274b1aa002c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function process(ContainerBuilder $container)
'provider',
'namespace',
'default_lifetime',
'reset',
);
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
$adapter = $pool = $container->getDefinition($id);
Expand Down Expand Up @@ -73,13 +74,19 @@ public function process(ContainerBuilder $container)
}
$i = 0;
foreach ($attributes as $attr) {
if (isset($tags[0][$attr]) && ('namespace' !== $attr || ArrayAdapter::class !== $adapter->getClass())) {
if (!isset($tags[0][$attr])) {
// no-op
} elseif ('reset' === $attr) {
if ($tags[0][$attr]) {
$pool->addTag('kernel.reset', array('method' => $tags[0][$attr]));
}
} elseif ('namespace' !== $attr || ArrayAdapter::class !== $adapter->getClass()) {
$pool->replaceArgument($i++, $tags[0][$attr]);
}
unset($tags[0][$attr]);
}
if (!empty($tags[0])) {
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]))));
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]))));
}

if (null !== $clearer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\DirectoryResource;
Expand Down Expand Up @@ -336,6 +337,8 @@ public function load(array $configs, ContainerBuilder $container)
->addTag('kernel.cache_warmer');
$container->registerForAutoconfiguration(EventSubscriberInterface::class)
->addTag('kernel.event_subscriber');
$container->registerForAutoconfiguration(ResettableInterface::class)
->addTag('kernel.reset', array('method' => 'reset'));
$container->registerForAutoconfiguration(PropertyListExtractorInterface::class)
->addTag('property_info.list_extractor');
$container->registerForAutoconfiguration(PropertyTypeExtractorInterface::class)
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<defaults public="false" />

<service id="cache.app" parent="cache.adapter.filesystem" public="true">
<tag name="cache.pool" clearer="cache.app_clearer" />
<tag name="cache.pool" clearer="cache.app_clearer" reset="reset" />
</service>

<service id="cache.system" parent="cache.adapter.system" public="true">
Expand Down Expand Up @@ -90,7 +90,7 @@
</service>

<service id="cache.adapter.memcached" class="Symfony\Component\Cache\Adapter\MemcachedAdapter" abstract="true">
<tag name="cache.pool" provider="cache.default_memcached_provider" clearer="cache.default_clearer" />
<tag name="cache.pool" provider="cache.default_memcached_provider" clearer="cache.default_clearer" reset="reset" />
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- Memcached connection service -->
<argument /> <!-- namespace -->
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
use Psr\Log\NullLogger;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\AbstractTrait;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
{
use AbstractTrait;

Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ArrayTrait;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
{
use ArrayTrait;

Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;

/**
* Chains several adapters together.
Expand All @@ -25,7 +26,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ChainAdapter implements AdapterInterface, PruneableInterface
class ChainAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
{
private $adapters = array();
private $adapterCount;
Expand Down Expand Up @@ -248,4 +249,16 @@ public function prune()

return $pruned;
}

/**
* {@inheritdoc}
*/
public function reset()
{
foreach ($this->adapters as $adapter) {
if ($adapter instanceof ResettableInterface) {
$adapter->reset();
}
}
}
}
22 changes: 12 additions & 10 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\PhpArrayTrait;

/**
Expand All @@ -24,7 +26,7 @@
* @author Titouan Galopin <galopintitouan@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
class PhpArrayAdapter implements AdapterInterface
class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
{
use PhpArrayTrait;

Expand All @@ -37,7 +39,7 @@ class PhpArrayAdapter implements AdapterInterface
public function __construct($file, AdapterInterface $fallbackPool)
{
$this->file = $file;
$this->fallbackPool = $fallbackPool;
$this->pool = $fallbackPool;
$this->zendDetectUnicode = ini_get('zend.detect_unicode');
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit) {
Expand Down Expand Up @@ -89,7 +91,7 @@ public function getItem($key)
$this->initialize();
}
if (!isset($this->values[$key])) {
return $this->fallbackPool->getItem($key);
return $this->pool->getItem($key);
}

$value = $this->values[$key];
Expand Down Expand Up @@ -144,7 +146,7 @@ public function hasItem($key)
$this->initialize();
}

return isset($this->values[$key]) || $this->fallbackPool->hasItem($key);
return isset($this->values[$key]) || $this->pool->hasItem($key);
}

/**
Expand All @@ -159,7 +161,7 @@ public function deleteItem($key)
$this->initialize();
}

return !isset($this->values[$key]) && $this->fallbackPool->deleteItem($key);
return !isset($this->values[$key]) && $this->pool->deleteItem($key);
}

/**
Expand All @@ -186,7 +188,7 @@ public function deleteItems(array $keys)
}

if ($fallbackKeys) {
$deleted = $this->fallbackPool->deleteItems($fallbackKeys) && $deleted;
$deleted = $this->pool->deleteItems($fallbackKeys) && $deleted;
}

return $deleted;
Expand All @@ -201,7 +203,7 @@ public function save(CacheItemInterface $item)
$this->initialize();
}

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

/**
Expand All @@ -213,15 +215,15 @@ public function saveDeferred(CacheItemInterface $item)
$this->initialize();
}

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

/**
* {@inheritdoc}
*/
public function commit()
{
return $this->fallbackPool->commit();
return $this->pool->commit();
}

/**
Expand Down Expand Up @@ -259,7 +261,7 @@ private function generateItems(array $keys)
}

if ($fallbackKeys) {
foreach ($this->fallbackPool->getItems($fallbackKeys) as $key => $item) {
foreach ($this->pool->getItems($fallbackKeys) as $key => $item) {
yield $key => $item;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ProxyTrait;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ProxyAdapter implements AdapterInterface
class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
{
private $pool;
use ProxyTrait;

private $namespace;
private $namespaceLen;
private $createCacheItem;
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
namespace Symfony\Component\Cache\Adapter;

use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ProxyTrait;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class SimpleCacheAdapter extends AbstractAdapter
class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
{
private $pool;
use ProxyTrait;

private $miss;

public function __construct(CacheInterface $pool, $namespace = '', $defaultLifetime = 0)
Expand Down
Loading