diff --git a/core/lib/Drupal/Core/Cache/CacheTagsInvalidationStorageInterface.php b/core/lib/Drupal/Core/Cache/CacheTagsInvalidationStorageInterface.php index d177ed4..f38b29c 100644 --- a/core/lib/Drupal/Core/Cache/CacheTagsInvalidationStorageInterface.php +++ b/core/lib/Drupal/Core/Cache/CacheTagsInvalidationStorageInterface.php @@ -15,7 +15,7 @@ /** * Returns the sum total of validations for a given set of tags. * - * @param array $tags + * @param string[] $tags * Array of cache tags. * * @return int @@ -26,7 +26,7 @@ public function checksumTags(array $tags); /** * Notifies the cache tag invalidation storage about cache tags being written. * - * @param array $tags + * @param string[] $tags * Array of cache tags. */ public function onCacheTagsWrite(array $tags); diff --git a/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php b/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php index 4b1c18d..543d195 100644 --- a/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php +++ b/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php @@ -17,7 +17,7 @@ class CacheTagsInvalidator implements CacheTagsInvalidatorInterface { use ContainerAwareTrait; /** - * Holds an array of cache tag invalidators. + * Holds an array of cache tags invalidators. * * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface[] */ @@ -30,22 +30,19 @@ public function invalidateTags(array $tags) { // Validate the tags. Cache::validateTags($tags); - // Notify all added cache tag validators. + // Notify all added cache tags invalidators. foreach ($this->invalidators as $invalidator) { $invalidator->invalidateTags($tags); } // Additionally, notify each cache bin if it implements the service. - foreach ($this->getBins() as $bin) { - if ($bin instanceof CacheTagsInvalidatorInterface) { - $bin->invalidateTags($tags); - } + foreach ($this->getInvalidatorCacheBins() as $bin) { + $bin->invalidateTags($tags); } - } /** - * Adds a cache tag invalidator. + * Adds a cache tags invalidator. * * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator * A cache invalidator. @@ -55,15 +52,19 @@ public function addInvalidator(CacheTagsInvalidatorInterface $invalidator) { } /** - * Gets all cache bin services. + * Returns all cache bins that need to be notified about invalidations. * - * @return \Drupal\Core\Cache\CacheBackendInterface - * An array of cache backend objects keyed by cache bins. + * @return \Drupal\Core\Cache\CacheTagsInvalidatorInterface[] + * An array of cache backend objects that implement the invalidator interface, + * keyed by their cache bin. */ - protected function getBins() { + protected function getInvalidatorCacheBins() { $bins = array(); foreach ($this->container->getParameter('cache_bins') as $service_id => $bin) { - $bins[$bin] = $this->container->get($service_id); + $service = $this->container->get($service_id); + if ($service instanceof CacheTagsInvalidatorInterface) { + $bins[$bin] = $service; + } } return $bins; } diff --git a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php index 70794b2..c261b84 100644 --- a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php @@ -53,7 +53,7 @@ public function __construct(Settings $settings = NULL, $consistent_service_name } // Default the fast backend to APCu if it's available. - if (FALSE && !isset($fast_service_name) && function_exists('apc_fetch')) { + if (!isset($fast_service_name) && function_exists('apc_fetch')) { $fast_service_name = 'cache.backend.apcu'; } diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 181dbd7..10445e4 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -35,7 +35,7 @@ class DatabaseBackend implements CacheBackendInterface { protected $connection; /** - * Storage for cache tag invalidations. + * Cache tag invalidations storage. * * @var \Drupal\Core\Cache\CacheTagsInvalidationStorageInterface */ @@ -406,9 +406,9 @@ protected function ensureBinExists() { /** * Act on an exception when cache might be stale. * - * If the table does not yet exist, that's fine, but if the table - * exists and yet the query failed, then the cache is stale and the - * exception needs to propagate. + * If the table does not yet exist, that's fine, but if the table exists and + * yet the query failed, then the cache is stale and the exception needs to + * propagate. * * @param $e * The exception. @@ -444,7 +444,7 @@ protected function normalizeCid($cid) { } /** - * Defines the schema for the {cache_*} bin table. + * Defines the schema for the {cache_*} bin tables. */ public function schemaDefinition() { $schema = array( diff --git a/core/lib/Drupal/Core/Cache/DatabaseCacheTagsStorage.php b/core/lib/Drupal/Core/Cache/DatabaseCacheTagsStorage.php index 9823227..db4363a 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseCacheTagsStorage.php +++ b/core/lib/Drupal/Core/Cache/DatabaseCacheTagsStorage.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Cache; -use \Drupal\Core\Database\Connection; +use Drupal\Core\Database\Connection; use Drupal\Core\Database\SchemaObjectExistsException; /** @@ -39,7 +39,7 @@ class DatabaseCacheTagsStorage implements CacheTagsInvalidationStorageInterface, protected $invalidatedTags = array(); /** - * Constructs a DatabaseBackend object. + * Constructs a DatabaseCacheTagsStorage object. * * @param \Drupal\Core\Database\Connection $connection * The database connection. @@ -69,8 +69,8 @@ public function invalidateTags(array $tags) { } catch (\Exception $e) { // Create the cache table, which will be empty. This fixes cases during - // core install where a cache table is cleared before it is set - // with {cache_render} and {cache_data}. + // core install where cache tags are invalidated before the table is + // created. if (!$this->ensureTableExists()) { $this->catchException($e); } @@ -112,7 +112,7 @@ public function checksumTags(array $tags) { * {@inheritdoc} */ public function onCacheTagsWrite(array $tags) { - // Remove tags that were already invalidated during this request/ from the + // Remove tags that were already invalidated during this request from the // static caches so that another deletion or invalidation can occur. foreach ($tags as $tag) { unset($this->invalidatedTags[$tag]); @@ -140,10 +140,10 @@ protected function ensureTableExists() { return TRUE; } - // If another process has already created the cache tags table, attempting to - // recreate it will throw an exception. In this case just catch the - // exception and do nothing. } + // If another process has already created the cachetags table, attempting to + // recreate it will throw an exception. In this case just catch the + // exception and do nothing. catch (SchemaObjectExistsException $e) { return TRUE; } @@ -155,7 +155,7 @@ protected function ensureTableExists() { */ public function schemaDefinition() { $schema = array( - 'description' => 'Cache table for tracking cache tags related to the cache bin.', + 'description' => 'Cache table for tracking cache tag invalidations.', 'fields' => array( 'tag' => array( 'description' => 'Namespace-prefixed tag string.', @@ -193,4 +193,5 @@ protected function catchException(\Exception $e) { throw $e; } } + } diff --git a/core/lib/Drupal/Core/Cache/PhpBackend.php b/core/lib/Drupal/Core/Cache/PhpBackend.php index 99a8bc9..ce120b9 100644 --- a/core/lib/Drupal/Core/Cache/PhpBackend.php +++ b/core/lib/Drupal/Core/Cache/PhpBackend.php @@ -132,7 +132,7 @@ protected function prepareItem($cache, $allow_invalid) { // Check expire time. $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME; - // Check if invalidateTags() has been called with any of the entry's tags. + // Check if invalidateTags() has been called with any of the item's tags. $checksum = $this->cacheTagsStorage->checksumTags($cache->tags); if ($cache->checksum_invalidations != $checksum) { $cache->valid = FALSE; diff --git a/core/modules/system/core.api.php b/core/modules/system/core.api.php index 70b4e1b..763836d 100644 --- a/core/modules/system/core.api.php +++ b/core/modules/system/core.api.php @@ -495,8 +495,7 @@ * ); * \Drupal::cache()->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags); * - * // Delete or invalidate all cache items with certain tags. - * \Drupal\Core\Cache\Cache::invalidateTags(array('node:1')); + * // nvalidate all cache items with certain tags. * \Drupal\Core\Cache\Cache::invalidateTags(array('user:1')); * @endcode * diff --git a/core/modules/views/tests/src/Unit/ViewsDataTest.php b/core/modules/views/tests/src/Unit/ViewsDataTest.php index d3e2a07..9002420 100644 --- a/core/modules/views/tests/src/Unit/ViewsDataTest.php +++ b/core/modules/views/tests/src/Unit/ViewsDataTest.php @@ -26,7 +26,7 @@ class ViewsDataTest extends UnitTestCase { protected $cacheBackend; /** - * The mocked cache tag invalidator. + * The mocked cache tags invalidator. * * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -66,7 +66,7 @@ class ViewsDataTest extends UnitTestCase { protected function setUp() { $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); - $this->getContainerWithCacheBins($this->cacheTagsInvalidator); + $this->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator); $configs = array(); $configs['views.settings']['skip_cache'] = FALSE; diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php index cf3962e..7e91fcb 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php @@ -19,14 +19,14 @@ class CacheCollectorTest extends UnitTestCase { /** * The cache backend that should be used. * - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $cacheBackend; /** - * The cache tag invalidator. + * The cache tags invalidator. * - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $cacheTagsInvalidator; @@ -61,7 +61,7 @@ protected function setUp() { $this->cid = $this->randomMachineName(); $this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock); - $this->getContainerWithCacheBins($this->cacheTagsInvalidator); + $this->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator); } diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php index 80da113..adfb7a3 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\Core\Cache; use Drupal\Core\Cache\CacheTagsInvalidator; +use Drupal\Core\DependencyInjection\Container; use Drupal\Tests\UnitTestCase; /** @@ -23,9 +24,46 @@ class CacheTagsInvalidatorTest extends UnitTestCase { * @expectedException \LogicException * @expectedExceptionMessage Cache tags must be strings, array given. */ - public function testInvalidateTags() { + public function testInvalidateTagsWithInvalidTags() { $cache_tags_invalidator = new CacheTagsInvalidator(); $cache_tags_invalidator->invalidateTags(['node' => [2, 3, 5, 8, 13]]); } + /** + * @covers ::invalidateTags + * @covers ::addInvalidator + * @covers ::getBins + */ + public function testInvalidateTags() { + $cache_tags_invalidator = new CacheTagsInvalidator(); + + // This does not actually implement, + // \Drupal\Cache\Cache\CacheBackendInterface but we can not mock from two + // interfaces, we would need a test class for that. + $invalidator_cache_bin = $this->getMock('\Drupal\Core\Cache\CacheTagsInvalidator'); + $invalidator_cache_bin->expects($this->once()) + ->method('invalidateTags') + ->with(array('node:1')); + + // We do not have to define that invalidateTags() is never called as the + // interface does not define that method, trying to call it would result in + // a fatal error. + $non_invalidator_cache_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendI'); + + $container = new Container(); + $container->set('cache.invalidator_cache_bin', $invalidator_cache_bin); + $container->set('cache.non_invalidator_cache_bin', $non_invalidator_cache_bin); + $container->setParameter('cache_bins', array('cache.invalidator_cache_bin' => 'invalidator_cache_bin', 'cache.non_invalidator_cache_bin' => 'non_invalidator_cache_bin')); + $cache_tags_invalidator->setContainer($container); + + $invalidator = $this->getMock('\Drupal\Core\Cache\CacheTagsInvalidator'); + $invalidator->expects($this->once()) + ->method('invalidateTags') + ->with(array('node:1')); + + $cache_tags_invalidator->addInvalidator($invalidator); + + $cache_tags_invalidator->invalidateTags(array('node:1')); + } + } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index 4873602..64f2603 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -66,7 +66,7 @@ class EntityManagerTest extends UnitTestCase { protected $cacheBackend; /** - * The cache tag invalidator. + * The cache tags invalidator. * * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -147,7 +147,7 @@ protected function setUp() { $this->formBuilder = $this->getMock('Drupal\Core\Form\FormBuilderInterface'); $this->controllerResolver = $this->getClassResolverStub(); - $this->container = $this->getContainerWithCacheBins($this->cacheTagsInvalidator); + $this->container = $this->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator); $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface'); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php index 9eb4660..4ab0366 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -73,7 +73,7 @@ class EntityUnitTest extends UnitTestCase { protected $languageManager; /** - * The mocked cache backend. + * The mocked cache tags invalidator. * * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject */ diff --git a/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php index fb10b99..34127c5 100644 --- a/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php @@ -68,7 +68,7 @@ class KeyValueEntityStorageTest extends UnitTestCase { protected $entityManager; /** - * The mocked cache backend. + * The mocked cache tags invalidator. * * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject */ diff --git a/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php b/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php index f8913db..2c9a015 100644 --- a/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php @@ -121,8 +121,8 @@ protected function setUp() { $logger = $this->getMock('Psr\Log\LoggerInterface'); $this->themeHandler = new TestThemeHandler($this->root, $this->configFactory, $this->moduleHandler, $this->state, $this->infoParser, $logger, $this->cssCollectionOptimizer, $this->configInstaller, $this->configManager, $this->routeBuilderIndicator, $this->extensionDiscovery); - $cache_backend = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); - $this->getContainerWithCacheBins($cache_backend); + $cache_tags_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); + $this->getContainerWithCacheTagsInvalidator($cache_tags_invalidator); } /** diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index 2cd5f30..9cd9375 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -200,7 +200,7 @@ public function testCacheClearWithTags() { ->expects($this->never()) ->method('deleteMultiple'); - $this->getContainerWithCacheBins($cache_tags_invalidator); + $this->getContainerWithCacheTagsInvalidator($cache_tags_invalidator); $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); $plugin_manager->setCacheBackend($cache_backend, $cid, array('tag')); diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index 015b3bb..ffc5a27 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -196,15 +196,15 @@ public function getStringTranslationStub() { } /** - * Sets up a container with cache bins. + * Sets up a container with a cache tags invalidator. * * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_handler - * The cache backend to set up. + * The cache tags invalidator. * * @return \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject - * The container with the cache bins set up. + * The container with the cache tags invalidator service. */ - protected function getContainerWithCacheBins(CacheTagsInvalidatorInterface $cache_tags_handler) { + protected function getContainerWithCacheTagsInvalidator(CacheTagsInvalidatorInterface $cache_tags_handler) { $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); $container->expects($this->any()) ->method('get')