diff --git a/core/core.services.yml b/core/core.services.yml index a77b0fd..e6395ee 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -28,16 +28,16 @@ services: - { name: cache.context} cache.backend.database: class: Drupal\Core\Cache\DatabaseBackendFactory - arguments: ['@database', '@cache_tag_factory'] + arguments: ['@database', '@cache_tag'] cache.backend.apcu: class: Drupal\Core\Cache\ApcuBackendFactory - arguments: ['@cache_tag_factory'] + arguments: ['@cache_tag'] cache.backend.php: class: Drupal\Core\Cache\PhpBackendFactory - arguments: ['@cache_tag_factory'] + arguments: ['@cache_tag'] cache.backend.memory: class: Drupal\Core\Cache\MemoryBackendFactory - arguments: ['@cache_tag_factory'] + arguments: ['@cache_tag'] cache.backend.null: class: Drupal\Core\Cache\NullBackendFactory cache.bootstrap: @@ -82,20 +82,9 @@ services: factory_method: get factory_service: cache_factory arguments: [discovery] - cache_tag_factory: - class: Drupal\Core\Cache\CacheTagBackendFactory - arguments: ['@settings'] - calls: - - [setContainer, ['@service_container']] - cache.tag.database: + cache_tag: class: Drupal\Core\Cache\DatabaseTagBackend arguments: ['@database'] - tags: - - { name: cache.tag } - cache.tag.memory: - class: Drupal\Core\Cache\MemoryTagBackend - tags: - - { name: cache.tag } config.cachedstorage.storage: class: Drupal\Core\Config\FileStorage factory_class: Drupal\Core\Config\FileStorageFactory diff --git a/core/lib/Drupal/Core/Cache/Cache.php b/core/lib/Drupal/Core/Cache/Cache.php index 2ef79ee..3c7f50b 100644 --- a/core/lib/Drupal/Core/Cache/Cache.php +++ b/core/lib/Drupal/Core/Cache/Cache.php @@ -24,55 +24,21 @@ class Cache { /** * Deletes items from all bins with any of the specified tags. * - * Many sites have more than one active cache backend, and each backend may - * use a different strategy for storing tags against cache items, and - * deleting cache items associated with a given tag. - * - * When deleting a given list of tags, we iterate over each cache backend, and - * and call deleteTags() on each. - * * @param array $tags * The list of tags to delete cache items for. */ public static function deleteTags(array $tags) { - foreach (static::getTags() as $tag_service) { - $tag_service->deleteTags($tags); - } + \Drupal::service('cache_tag')->deleteTags($tags); } /** * Marks cache items from all bins with any of the specified tags as invalid. * - * Many sites have more than one active cache backend, and each backend my use - * a different strategy for storing tags against cache items, and invalidating - * cache items associated with a given tag. - * - * When invalidating a given list of tags, we iterate over each cache backend, - * and call invalidateTags() on each. - * * @param array $tags * The list of tags to invalidate cache items for. */ public static function invalidateTags(array $tags) { - foreach (static::getTags() as $tag_service) { - $tag_service->invalidateTags($tags); - } - } - - /** - * Gets all cache tag services. - * - * @return array - * An array of cache tag objects keyed by service name. - */ - public static function getTags() { - $tags = array(); - $container = \Drupal::getContainer(); - foreach ($container->getParameter('cache_tags') as $service_id => $tag) { - $tags[$tag] = $container->get($service_id); - } - - return $tags; + \Drupal::service('cache_tag')->invalidateTags($tags); } /** diff --git a/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php b/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php index 6e224a1..ca31af1 100644 --- a/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php +++ b/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php @@ -52,6 +52,7 @@ public function flattenTags(array $tags); * Associative array of tags. * @param bool $set_context * TRUE if called from set context. + * * @return array * Array with two items (invalidations, deletions) providing sum of all * invalidations/deletions over all provided tags. diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index aba8482..034b5b6 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -172,8 +172,8 @@ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array */ protected function doSet($cid, $data, $expire, $tags) { $flat_tags = $this->cacheTag->flattenTags($tags); - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::deletedTags', array()); - $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::invalidatedTags', array()); + $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseTagBackend::deletedTags', array()); + $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseTagBackend::invalidatedTags', array()); // Remove tags that were already deleted or invalidated during this request // from the static caches so that another deletion or invalidation can // occur. @@ -214,8 +214,8 @@ protected function doSet($cid, $data, $expire, $tags) { * {@inheritdoc} */ public function setMultiple(array $items) { - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::deletedTags', array()); - $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::invalidatedTags', array()); + $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseTagBackend::deletedTags', array()); + $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseTagBackend::invalidatedTags', array()); // Use a transaction so that the database can write the changes in a single // commit. @@ -236,7 +236,7 @@ public function setMultiple(array $items) { 'tags' => array(), ); - $flat_tags = $this->flattenTags($item['tags']); + $flat_tags = $this->cacheTag->flattenTags($item['tags']); // Remove tags that were already deleted or invalidated during this // request from the static caches so that another deletion or @@ -250,7 +250,7 @@ public function setMultiple(array $items) { } } - $checksum = $this->checksumTags($flat_tags); + $checksum = $this->cacheTag->checksumTags($flat_tags, TRUE); $fields = array( 'cid' => $cid, diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 7046026..9862d21 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -19,20 +19,20 @@ class DatabaseBackendFactory implements CacheFactoryInterface { protected $connection; /** - * The cache tag factory service. + * The cache tag service. * - * @var \Drupal\Core\Cache\CacheTagBackendFactory + * @var \Drupal\Core\Cache\CacheTagBackendInterface */ - protected $cacheTagFactory; + protected $cacheTagBackend; /** * Constructs the DatabaseBackendFactory object. * * @param \Drupal\Core\Database\Connection $connection */ - function __construct(Connection $connection, CacheTagBackendFactory $cache_tag_factory) { + function __construct(Connection $connection, CacheTagBackendBase $cache_tag_backend) { $this->connection = $connection; - $this->cacheTagFactory = $cache_tag_factory; + $this->cacheTagBackend = $cache_tag_backend; } /** @@ -45,7 +45,7 @@ function __construct(Connection $connection, CacheTagBackendFactory $cache_tag_f * The cache backend object for the specified cache bin. */ function get($bin) { - return new DatabaseBackend($this->connection, $this->cacheTagFactory->get(), $bin); + return new DatabaseBackend($this->connection, $this->cacheTagBackend, $bin); } } diff --git a/core/lib/Drupal/Core/Cache/ListCacheBinsPass.php b/core/lib/Drupal/Core/Cache/ListCacheBinsPass.php index 2031aed..08c29fe 100644 --- a/core/lib/Drupal/Core/Cache/ListCacheBinsPass.php +++ b/core/lib/Drupal/Core/Cache/ListCacheBinsPass.php @@ -11,7 +11,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; /** - * Adds cache_bins and cache_tags parameters to the container. + * Adds cache_bins parameter to the container. */ class ListCacheBinsPass implements CompilerPassInterface { @@ -19,12 +19,10 @@ class ListCacheBinsPass implements CompilerPassInterface { * {@inheritdoc} */ public function process(ContainerBuilder $container) { - foreach (array('cache.bin' => 'cache_bins', 'cache.tag' => 'cache_tags') as $name => $param) { - $params = array(); - foreach ($container->findTaggedServiceIds($name) as $id => $attributes) { - $params[$id] = substr($id, strpos($id, '.') + 1); - } - $container->setParameter($param, $params); + $cache_bins = array(); + foreach ($container->findTaggedServiceIds('cache.bin') as $id => $attributes) { + $cache_bins[$id] = substr($id, strpos($id, '.') + 1); } + $container->setParameter('cache_bins', $cache_bins); } } diff --git a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php index acaa82f..093b373 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php @@ -12,24 +12,24 @@ class MemoryBackendFactory implements CacheFactoryInterface { /** * The cache tag factory service. * - * @var \Drupal\Core\Cache\CacheTagBackendFactory + * @var \Drupal\Core\Cache\CacheTagBackendInterface */ - protected $cacheTagFactory; + protected $cacheTagBackend; /** * Constructs the DatabaseBackendFactory object. * * @param \Drupal\Core\Database\Connection $connection */ - function __construct(CacheTagBackendFactory $cache_tag_factory) { - $this->cacheTagFactory = $cache_tag_factory; + function __construct(CacheTagBackendInterface $cache_tag_backend) { + $this->cacheTagBackend = $cache_tag_backend; } /** * {@inheritdoc} */ function get($bin) { - return new MemoryBackend($this->cacheTagFactory->get(), $bin); + return new MemoryBackend($this->cacheTagBackend, $bin); } } diff --git a/core/lib/Drupal/Core/DependencyInjection/UpdateServiceProvider.php b/core/lib/Drupal/Core/DependencyInjection/UpdateServiceProvider.php index ce1fe6d..a2acee1 100644 --- a/core/lib/Drupal/Core/DependencyInjection/UpdateServiceProvider.php +++ b/core/lib/Drupal/Core/DependencyInjection/UpdateServiceProvider.php @@ -35,7 +35,10 @@ public function register(ContainerBuilder $container) { $container ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory') - ->addArgument(new Reference('cache_tag_factory')); + ->addArgument(new Reference('cache_tag')); + $container->getDefinition('cache_tag') + ->setClass('Drupal\Core\Cache\MemoryTagBackend') + ->setArguments(array()); $container ->register('router.builder', 'Drupal\Core\Routing\RouteBuilderStatic'); diff --git a/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php b/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php index a3f5de9..d70c522 100644 --- a/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php +++ b/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php @@ -31,14 +31,13 @@ public function register(ContainerBuilder $container) { $container->register('config.storage', 'Drupal\Core\Config\InstallStorage'); // Replace services with in-memory implementations. - // @todo Revert and inject cache tag service when refactored to avoid the - // cache tag factory. - foreach (array('bootstrap', 'discovery', 'default', 'data', 'render') as $bin) { - $container - ->register("cache.$bin", 'Drupal\Core\Cache\MemoryBackend') - ->addArgument(new Reference('cache.tag.memory')) - ->addArgument($bin); - } + $definition = $container->getDefinition('cache_factory'); + $definition->setClass('Drupal\Core\Cache\MemoryBackendFactory'); + $definition->setArguments(array(new Reference('cache_tag'))); + $definition->setMethodCalls(array()); + $definition = $container->getDefinition('cache_tag'); + $definition->setClass('Drupal\Core\Cache\MemoryTagBackend'); + $definition->setArguments(array()); $container ->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory'); $container diff --git a/core/modules/config/src/Tests/Storage/CachedStorageTest.php b/core/modules/config/src/Tests/Storage/CachedStorageTest.php index 766d013..87a7668 100644 --- a/core/modules/config/src/Tests/Storage/CachedStorageTest.php +++ b/core/modules/config/src/Tests/Storage/CachedStorageTest.php @@ -9,8 +9,8 @@ use Drupal\Core\Config\FileStorage; use Drupal\Core\Config\CachedStorage; -use Drupal\Core\Database\Database; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; /** * Tests CachedStorage operations. @@ -95,7 +95,8 @@ public function containerBuild(ContainerBuilder $container) { parent::containerBuild($container); // Use the regular database cache backend to aid testing. $container->register('cache_factory', 'Drupal\Core\Cache\DatabaseBackendFactory') - ->addArgument(Database::getConnection()); + ->addArgument(new Reference('database')) + ->addArgument(new Reference('cache_tag')); } } diff --git a/core/modules/simpletest/src/KernelTestBase.php b/core/modules/simpletest/src/KernelTestBase.php index d9daffb..4e29b14 100644 --- a/core/modules/simpletest/src/KernelTestBase.php +++ b/core/modules/simpletest/src/KernelTestBase.php @@ -236,11 +236,12 @@ public function containerBuild(ContainerBuilder $container) { // Set the default language on the minimal container. $this->container->setParameter('language.default_values', Language::$defaultValues); + $container->register('cache_tag', 'Drupal\Core\Cache\MemoryTagBackend'); + $container->register('lock', 'Drupal\Core\Lock\NullLockBackend'); $this->settingsSet('cache', array('default' => 'cache.backend.memory')); - $this->settingsSet('cache_tag_service', 'cache.tag.memory'); $container->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory') - ->addArgument(new Reference('cache_tag_factory'));; + ->addArgument(new Reference('cache_tag')); $container ->register('config.storage.active', 'Drupal\Core\Config\DatabaseStorage') diff --git a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php index a4e0c6d..1194db8 100644 --- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php @@ -36,7 +36,7 @@ public static function getInfo() { * A new DatabaseBackend object. */ protected function createCacheBackend($bin) { - return new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tag_factory')->get(), $bin); + return new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tag'), $bin); } } diff --git a/core/modules/system/src/Tests/DrupalKernel/DrupalKernelSiteTest.php b/core/modules/system/src/Tests/DrupalKernel/DrupalKernelSiteTest.php index d6f9fe8..19ae0c9 100644 --- a/core/modules/system/src/Tests/DrupalKernel/DrupalKernelSiteTest.php +++ b/core/modules/system/src/Tests/DrupalKernel/DrupalKernelSiteTest.php @@ -44,6 +44,7 @@ class: $class # Swap out a core service. cache.backend.database: class: Drupal\Core\Cache\MemoryBackendFactory + arguments: ['@cache_tag'] EOD; file_put_contents($this->siteDirectory . '/services.yml', $doc); diff --git a/core/modules/system/src/Tests/Plugin/PluginTestBase.php b/core/modules/system/src/Tests/Plugin/PluginTestBase.php index 3a92dbd..ea709ef 100644 --- a/core/modules/system/src/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/src/Tests/Plugin/PluginTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Plugin; +use Drupal\Core\Cache\MemoryTagBackend; use Drupal\simpletest\UnitTestBase; use Drupal\plugin_test\Plugin\TestPluginManager; use Drupal\plugin_test\Plugin\MockBlockManager; @@ -38,7 +39,7 @@ public function setUp() { // as derivatives and ReflectionFactory. $this->testPluginManager = new TestPluginManager(); $this->mockBlockManager = new MockBlockManager(); - $module_handler = new ModuleHandler(array(), new MemoryBackend('plugin')); + $module_handler = new ModuleHandler(array(), new MemoryBackend(new MemoryTagBackend(), 'plugin')); $this->defaultsTestPluginManager = new DefaultsTestPluginManager($module_handler); // The expected plugin definitions within each manager. Several tests assert