diff --git a/core/lib/Drupal/Core/Cache/CacheTagBackendFactory.php b/core/lib/Drupal/Core/Cache/CacheTagBackendFactory.php deleted file mode 100644 index 099b503..0000000 --- a/core/lib/Drupal/Core/Cache/CacheTagBackendFactory.php +++ /dev/null @@ -1,57 +0,0 @@ -settings = $settings; - } - - /** - * Instantiates a cache tag class. - * - * By default, this returns an instance of the - * Drupal\Core\Cache\DatabaseBackendTag class. - * - * @return \Drupal\Core\Cache\CacheTagBackendInterface - * The cache tag instance. - */ - public function get() { - $cache_tag_service = $this->settings->get('cache_tag_service'); - - if (isset($cache_tag_service)) { - $service_name = $cache_tag_service; - } - else { - $service_name = 'cache.tag.database'; - } - - return $this->container->get($service_name); - } - -} diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 034b5b6..56ce716 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -172,19 +172,6 @@ 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\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. - foreach ($flat_tags as $tag) { - if (isset($deleted_tags[$tag])) { - unset($deleted_tags[$tag]); - } - if (isset($invalidated_tags[$tag])) { - unset($invalidated_tags[$tag]); - } - } $checksum = $this->cacheTag->checksumTags($flat_tags, TRUE); $fields = array( @@ -214,9 +201,6 @@ protected function doSet($cid, $data, $expire, $tags) { * {@inheritdoc} */ public function setMultiple(array $items) { - $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. $transaction = $this->connection->startTransaction(); @@ -237,19 +221,6 @@ public function setMultiple(array $items) { ); $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 - // invalidation can occur. - foreach ($flat_tags as $tag) { - if (isset($deleted_tags[$tag])) { - unset($deleted_tags[$tag]); - } - if (isset($invalidated_tags[$tag])) { - unset($invalidated_tags[$tag]); - } - } - $checksum = $this->cacheTag->checksumTags($flat_tags, TRUE); $fields = array( diff --git a/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php b/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php index 015d86d..8fd66c9 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Cache; +use Drupal\aggregator\Annotation\AggregatorFetcher; use Drupal\Core\Database\Connection; use Drupal\Core\Database\SchemaObjectExistsException; @@ -23,6 +24,31 @@ class DatabaseTagBackend extends CacheTagBackendBase { protected $connection; /** + * Static cache of cache tag information. + * + * @var array + */ + protected $tagCache = array(); + + /** + * List of cache tags that were already deleted. + * + * Used to avoid incrementing the deletion counter multiple times per request. + * + * @var array + */ + protected $deletedTags = array(); + + /** + * List of cache tags that were already deleted. + * + * Used to avoid incrementing the deletion counter multiple times per request. + * + * @var array + */ + protected $invalidatedTags = array(); + + /** * Constructs a DatabaseTag object. * * @param \Drupal\Core\Database\Connection $connection @@ -36,12 +62,10 @@ public function __construct(Connection $connection) { * {@inheritdoc} */ public function invalidateTags(array $tags) { - $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::invalidatedTags', array()); - $tag_cache = &drupal_static('Drupal\Core\Cache\CacheTagBackendInterface::tagCache', array()); try { foreach ($this->flattenTags($tags) as $tag) { // Only invalidate tags once per request unless they are written again. - if (isset($invalidated_tags[$tag])) { + if (isset($this->invalidatedTags[$tag])) { continue; } $this->connection->merge('cache_tags') @@ -50,8 +74,8 @@ public function invalidateTags(array $tags) { ->key(array('tag' => $tag)) ->execute(); - $invalidated_tags[$tag] = TRUE; - unset($tag_cache[$tag]); + $this->invalidatedTags[$tag] = TRUE; + unset($this->tagCache[$tag]); } } catch (\Exception $e) { @@ -68,13 +92,11 @@ public function invalidateTags(array $tags) { * {@inheritdoc} */ public function deleteTags(array $tags) { - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseTagBackend::deletedTags', array()); - $tag_cache = &drupal_static('Drupal\Core\Cache\CacheTagBackendInterface::tagCache', array()); try { foreach ($this->flattenTags($tags) as $tag) { // Only delete tags once per request unless they are written again. - if (isset($deleted_tags[$tag])) { + if (isset($this->deletedTags[$tag])) { continue; } $this->connection->merge('cache_tags') @@ -83,8 +105,8 @@ public function deleteTags(array $tags) { ->key(array('tag' => $tag)) ->execute(); - $deleted_tags[$tag] = TRUE; - unset($tag_cache[$tag]); + $this->deletedTags[$tag] = TRUE; + unset($this->tagCache[$tag]); } } @@ -103,15 +125,15 @@ public function deleteTags(array $tags) { * {@inheritdoc} */ public function checksumTags(array $tags, $set_context) { - $tag_cache = &drupal_static('Drupal\Core\Cache\CacheTagBackendInterface::tagCache', array()); - if ($set_context) { - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseTagBackend::deletedTags', array()); // Remove tags that were already deleted during this request from the static // cache so that another deletion for them will be correctly updated. foreach ($tags as $tag) { - if (isset($deleted_tags[$tag])) { - unset($deleted_tags[$tag]); + if (isset($this->deletedTags[$tag])) { + unset($this->deletedTags[$tag]); + } + if (isset($this->invalidatedTags[$tag])) { + unset($this->invalidatedTags[$tag]); } } } @@ -121,7 +143,7 @@ public function checksumTags(array $tags, $set_context) { 'deletions' => 0, ); - $query_tags = array_diff($tags, array_keys($tag_cache)); + $query_tags = array_diff($tags, array_keys($this->tagCache)); if ($query_tags) { try { $db_tags = $this->connection->query('SELECT tag, invalidations, deletions FROM {cache_tags} WHERE tag IN (:tags)', array(':tags' => $query_tags))->fetchAllAssoc('tag', \PDO::FETCH_ASSOC); @@ -134,15 +156,15 @@ public function checksumTags(array $tags, $set_context) { throw $e; } } - $tag_cache += $db_tags; + $this->tagCache += $db_tags; // Fill static cache with empty objects for tags not found in the database. - $tag_cache += array_fill_keys(array_diff($query_tags, array_keys($db_tags)), $checksum); + $this->tagCache += array_fill_keys(array_diff($query_tags, array_keys($db_tags)), $checksum); } foreach ($tags as $tag) { - $checksum['invalidations'] += $tag_cache[$tag]['invalidations']; - $checksum['deletions'] += $tag_cache[$tag]['deletions']; + $checksum['invalidations'] += $this->tagCache[$tag]['invalidations']; + $checksum['deletions'] += $this->tagCache[$tag]['deletions']; } return $checksum; @@ -167,7 +189,9 @@ public function prepareGet(&$item) { * {@inheritdoc}. */ public function clearCache() { - // Nothing to do here. + $this->tagCache = array(); + $this->invalidatedTags = array(); + $this->deletedTags = array(); } /** diff --git a/core/modules/locale/src/Tests/LocaleTranslationUiTest.php b/core/modules/locale/src/Tests/LocaleTranslationUiTest.php index 56e7605..67b807c 100644 --- a/core/modules/locale/src/Tests/LocaleTranslationUiTest.php +++ b/core/modules/locale/src/Tests/LocaleTranslationUiTest.php @@ -143,7 +143,7 @@ function testStringTranslation() { // Reset the tag cache on the tester side in order to pick up the call to // Cache::deleteTags() on the tested side. - $this->container->get('cache.tag.database')->clearCache(); + $this->container->get('cache_tag')->clearCache(); $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, 't() works for non-English.'); // Refresh the locale() cache to get fresh data from t() below. We are in diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 4e72de2..1b457d7 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -1172,10 +1172,7 @@ protected function resetAll() { */ protected function refreshVariables() { // Clear the tag cache. - $this->container->get('cache.tag.database')->clearCache(); - drupal_static_reset('Drupal\Core\Cache\CacheTagBackendInterface::tagCache'); - drupal_static_reset('Drupal\Core\Cache\DatabaseTagBackend::deletedTags'); - drupal_static_reset('Drupal\Core\Cache\DatabaseBackend::invalidatedTags'); + $this->container->get('cache.tag')->clearCache(); $this->container->get('config.factory')->reset(); $this->container->get('state')->resetCache();