diff --git a/core/core.services.yml b/core/core.services.yml index e6395ee..913db85 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -85,10 +85,6 @@ services: cache_tag: class: Drupal\Core\Cache\DatabaseTagBackend arguments: ['@database'] - config.cachedstorage.storage: - class: Drupal\Core\Config\FileStorage - factory_class: Drupal\Core\Config\FileStorageFactory - factory_method: getActive config.manager: class: Drupal\Core\Config\ConfigManager arguments: ['@entity.manager', '@config.factory', '@config.typed', '@string_translation', '@config.storage', '@event_dispatcher'] diff --git a/core/lib/Drupal/Core/Cache/ApcuBackend.php b/core/lib/Drupal/Core/Cache/ApcuBackend.php index ffaeba3..f9111bd 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackend.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php @@ -36,44 +36,27 @@ class ApcuBackend implements CacheBackendInterface { protected $binPrefix; /** - * Prefix for keys holding invalidation cache tags. + * The cache tag factory service. * - * Includes the site-specific prefix in $sitePrefix. - * - * @var string - */ - protected $invalidationsTagsPrefix; - - /** - * Prefix for keys holding invalidation cache tags. - * - * Includes the site-specific prefix in $sitePrefix. - * - * @var string - */ - protected $deletionsTagsPrefix; - - /** - * A static cache of all tags checked during the request. - * - * @var array + * @var \Drupal\Core\Cache\CacheTagBackendInterface */ - protected static $tagCache = array('deletions' => array(), 'invalidations' => array()); + protected $cacheTag; /** * Constructs a new ApcuBackend instance. * + * @param \Drupal\Core\Cache\CacheTagBackendInterface $cache_tag + * The cache tag service. * @param string $bin * The name of the cache bin. * @param string $site_prefix * The prefix to use for all keys in the storage that belong to this site. */ - public function __construct($bin, $site_prefix) { + public function __construct(CacheTagBackendInterface $cache_tag, $bin, $site_prefix) { + $this->cacheTag = $cache_tag; $this->bin = $bin; $this->sitePrefix = $site_prefix; $this->binPrefix = $this->sitePrefix . '::' . $this->bin . '::'; - $this->invalidationsTagsPrefix = $this->sitePrefix . '::itags::'; - $this->deletionsTagsPrefix = $this->sitePrefix . '::dtags::'; } /** @@ -161,19 +144,18 @@ protected function prepareItem($cache, $allow_invalid) { } $cache->tags = $cache->tags ? explode(' ', $cache->tags) : array(); - $checksum = $this->checksumTags($cache->tags); - - // Check if deleteTags() has been called with any of the entry's tags. - if ($cache->checksum_deletions != $checksum['deletions']) { - return FALSE; - } + $this->cacheTag->prepareGet($cache); // Check expire time. - $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME; + $cache->valid = $time_valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME; - // Check if invalidateTags() has been called with any of the entry's tags. - if ($cache->checksum_invalidations != $checksum['invalidations']) { - $cache->valid = FALSE; + if ($cache->deleted) { + $this->delete($cache->cid); + return FALSE; + } + + if ($time_valid && !$cache->valid) { + $this->invalidate($cache->cid); } if (!$allow_invalid && !$cache->valid) { @@ -191,8 +173,8 @@ public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANEN $cache->cid = $cid; $cache->created = round(microtime(TRUE), 3); $cache->expire = $expire; - $cache->tags = implode(' ', $this->flattenTags($tags)); - $checksum = $this->checksumTags($tags); + $cache->tags = implode(' ', $this->cacheTag->flattenTags($tags)); + $checksum = $this->cacheTag->checksumTags($tags, TRUE); $cache->checksum_invalidations = $checksum['invalidations']; $cache->checksum_deletions = $checksum['deletions']; // APC serializes/unserializes any structure itself. @@ -256,13 +238,6 @@ public function removeBin() { /** * {@inheritdoc} */ - public function isEmpty() { - return $this->getAll()->getTotalCount() === 0; - } - - /** - * {@inheritdoc} - */ public function invalidate($cid) { $this->invalidateMultiple(array($cid)); } @@ -286,91 +261,4 @@ public function invalidateAll() { } } - /** - * {@inheritdoc} - */ - public function deleteTags(array $tags) { - foreach ($this->flattenTags($tags) as $tag) { - apc_inc($this->deletionsTagsPrefix . $tag, 1, $success); - if (!$success) { - apc_store($this->deletionsTagsPrefix . $tag, 1); - } - } - } - - /** - * {@inheritdoc} - */ - public function invalidateTags(array $tags) { - foreach ($this->flattenTags($tags) as $tag) { - apc_inc($this->invalidationsTagsPrefix . $tag, 1, $success); - if (!$success) { - apc_store($this->invalidationsTagsPrefix . $tag, 1); - } - } - } - - /** - * Flattens a tags array into a numeric array suitable for string storage. - * - * @param array $tags - * Associative array of tags to flatten. - * - * @return array - * Indexed array of flattened tag identifiers. - */ - protected function flattenTags(array $tags) { - if (isset($tags[0])) { - return $tags; - } - - $flat_tags = array(); - foreach ($tags as $namespace => $values) { - if (is_array($values)) { - foreach ($values as $value) { - $flat_tags[] = "$namespace:$value"; - } - } - else { - $flat_tags[] = "$namespace:$values"; - } - } - return $flat_tags; - } - - /** - * Returns the sum total of validations for a given set of tags. - * - * @param array $tags - * Associative array of tags. - * - * @return int - * Sum of all invalidations. - */ - protected function checksumTags(array $tags) { - $checksum = array('invalidations' => 0, 'deletions' => 0); - $query_tags = array('invalidations' => array(), 'deletions' => array()); - - foreach ($this->flattenTags($tags) as $tag) { - foreach (array('deletions', 'invalidations') as $type) { - if (isset(static::$tagCache[$type][$tag])) { - $checksum[$type] += static::$tagCache[$type][$tag]; - } - else { - $query_tags[$type][] = $this->{$type . 'TagsPrefix'} . $tag; - } - } - } - - foreach (array('deletions', 'invalidations') as $type) { - if ($query_tags[$type]) { - $result = apc_fetch($query_tags[$type]); - static::$tagCache[$type] = array_merge(static::$tagCache[$type], $result); - $checksum[$type] += array_sum($result); - } - } - - return $checksum; - } - } diff --git a/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php b/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php index 341203c..0718848 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php @@ -19,9 +19,20 @@ class ApcuBackendFactory implements CacheFactoryInterface { protected $sitePrefix; /** + * The cache tag factory service. + * + * @var \Drupal\Core\Cache\CacheTagBackendInterface + */ + protected $cacheTagBackend; + + /** * Constructs an ApcuBackendFactory object. + * + * @param \Drupal\Core\Cache\CacheTagBackendInterface $cache_tag_backend + * The cache tag service. */ - public function __construct() { + function __construct(CacheTagBackendInterface $cache_tag_backend) { + $this->cacheTagBackend = $cache_tag_backend; $this->sitePrefix = Crypt::hashBase64(DRUPAL_ROOT . '/' . conf_path()); } @@ -35,7 +46,7 @@ public function __construct() { * The cache backend object for the specified cache bin. */ public function get($bin) { - return new ApcuBackend($bin, $this->sitePrefix); + return new ApcuBackend($this->cacheTagBackend, $bin, $this->sitePrefix); } } diff --git a/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php b/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php index ca31af1..6323e3b 100644 --- a/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php +++ b/core/lib/Drupal/Core/Cache/CacheTagBackendInterface.php @@ -65,7 +65,7 @@ public function checksumTags(array $tags, $set_context); * $item->deleted should be set to TRUE if item was deleted via tags and * $item->valid should be set to FALSE if item was invalidated. */ - public function prepareGet(&$item); + public function prepareGet($item); /** * Clears cache tag service internal caches. diff --git a/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php b/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php index 8fd66c9..526dd01 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseTagBackend.php @@ -173,7 +173,7 @@ public function checksumTags(array $tags, $set_context) { /** * {@inheritdoc} */ - public function prepareGet(&$item) { + public function prepareGet($item) { $checksum = $this->checksumTags($item->tags, FALSE); // Check if deleteTags() has been called with any of the entry's tags. diff --git a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php index 093b373..75ddb36 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php @@ -10,16 +10,17 @@ class MemoryBackendFactory implements CacheFactoryInterface { /** - * The cache tag factory service. + * The cache tag service. * * @var \Drupal\Core\Cache\CacheTagBackendInterface */ protected $cacheTagBackend; /** - * Constructs the DatabaseBackendFactory object. + * Constructs the MemoryBackendFactory object. * - * @param \Drupal\Core\Database\Connection $connection + * @param \Drupal\Core\Cache\CacheTagBackendInterface $cache_tag_backend + * The cache tag service. */ function __construct(CacheTagBackendInterface $cache_tag_backend) { $this->cacheTagBackend = $cache_tag_backend; diff --git a/core/lib/Drupal/Core/Cache/MemoryTagBackend.php b/core/lib/Drupal/Core/Cache/MemoryTagBackend.php index 4920b05..9bbc8cb 100644 --- a/core/lib/Drupal/Core/Cache/MemoryTagBackend.php +++ b/core/lib/Drupal/Core/Cache/MemoryTagBackend.php @@ -8,9 +8,10 @@ namespace Drupal\Core\Cache; /** - * Class MemoryTagBackend. + * Memory cache tags implementation. */ class MemoryTagBackend extends CacheTagBackendBase { + /** * The cache tag storage. * @@ -74,7 +75,7 @@ public function checksumTags(array $tags, $set_context) { /** * {@inheritdoc} */ - public function prepareGet(&$item) { + public function prepareGet($item) { $checksum = $this->checksumTags($item->tags, FALSE); // Check if deleteTags() has been called with any of the entry's tags. diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 1b457d7..988bfa9 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -868,7 +868,6 @@ protected function setUp() { // Execute the non-interactive installer. require_once DRUPAL_ROOT . '/core/includes/install.core.inc'; $this->settingsSet('cache', array('default' => 'cache.backend.memory')); - $this->settingsSet('cache_tag_service', 'cache.tag.memory'); $parameters = $this->installParameters(); install_drupal($parameters); @@ -1172,7 +1171,7 @@ protected function resetAll() { */ protected function refreshVariables() { // Clear the tag cache. - $this->container->get('cache.tag')->clearCache(); + $this->container->get('cache_tag')->clearCache(); $this->container->get('config.factory')->reset(); $this->container->get('state')->resetCache(); diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index 1dc8b0b..65000a1 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -217,8 +217,7 @@ public function getStringTranslationStub() { */ protected function getContainerWithCacheTags(CacheTagBackendInterface $backend) { $container = new ContainerBuilder(); - $container->setParameter('cache_tags', array('cache.tag.test' => 'test')); - $container->set('cache.tag.test', $backend); + $container->set('cache_tag', $backend); \Drupal::setContainer($container); return $container;