commit 82e1b8d96ce0105198a5244442d62df375ec7ce2 Author: Pieter Frenssen Date: Sat Jul 4 15:08:46 2015 +0200 23 diff --git a/core/lib/Drupal/Core/Cache/RuntimeCacheContextDependencyInterface.php b/core/lib/Drupal/Core/Cache/RuntimeCacheableDependencyInterface.php similarity index 35% rename from core/lib/Drupal/Core/Cache/RuntimeCacheContextDependencyInterface.php rename to core/lib/Drupal/Core/Cache/RuntimeCacheableDependencyInterface.php index 1c84991..03d88d4 100644 --- a/core/lib/Drupal/Core/Cache/RuntimeCacheContextDependencyInterface.php +++ b/core/lib/Drupal/Core/Cache/RuntimeCacheableDependencyInterface.php @@ -2,17 +2,17 @@ /** * @file - * Contains \Drupal\Core\Cache\RuntimeCacheContextDependencyInterface. + * Contains \Drupal\Core\Cache\RuntimeCacheableDependencyInterface. */ namespace Drupal\Core\Cache; /** - * Defines an interface for objects that can add cache contexts during runtime. + * Interface for objects that can change cache dependencies during runtime. * * @ingroup cache */ -interface RuntimeCacheContextDependencyInterface { +interface RuntimeCacheableDependencyInterface { /** * Adds cache contexts during runtime. @@ -22,4 +22,20 @@ */ public function addRuntimeCacheContexts(array $cache_contexts); + /** + * Adds cache tags during runtime. + * + * @param string[] $cache_tags + * An array of cache tags to add. + */ + public function addRuntimeCacheTags(array $cache_tags); + + /** + * Sets the cache max age during runtime. + * + * @param int $max_age + * The cache max-age value. + */ + public function setRuntimeCacheMaxAge($max_age); + } diff --git a/core/lib/Drupal/Core/Config/ConfigBase.php b/core/lib/Drupal/Core/Config/ConfigBase.php index 9fc9cdf..556926d 100644 --- a/core/lib/Drupal/Core/Config/ConfigBase.php +++ b/core/lib/Drupal/Core/Config/ConfigBase.php @@ -11,7 +11,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; -use Drupal\Core\Cache\RuntimeCacheContextDependencyInterface; +use Drupal\Core\Cache\RuntimeCacheableDependencyInterface; use \Drupal\Core\DependencyInjection\DependencySerializationTrait; /** @@ -29,7 +29,7 @@ * @see \Drupal\Core\Config\Config * @see \Drupal\Core\Theme\ThemeSettings */ -abstract class ConfigBase implements CacheableDependencyInterface, RuntimeCacheContextDependencyInterface { +abstract class ConfigBase implements CacheableDependencyInterface, RuntimeCacheableDependencyInterface { use DependencySerializationTrait; /** @@ -49,11 +49,25 @@ /** * The cache contexts this configuration object provides. * - * @var array + * @var string[] */ protected $cacheContexts = []; /** + * The cache tags this configuration object provides. + * + * @var string[] + */ + protected $cacheTags = []; + + /** + * The cache max age this configuration object provides. + * + * @var int + */ + protected $cacheMaxAge; + + /** * The maximum length of a configuration object name. * * Many filesystems (including HFS, NTFS, and ext4) have a maximum file name @@ -301,4 +315,18 @@ public function addRuntimeCacheContexts(array $cache_contexts) { $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts); } + /** + * {@inheritdoc} + */ + public function addRuntimeCacheTags(array $cache_tags) { + $this->cacheTags = Cache::mergeTags($this->cacheTags, $cache_tags); + } + + /** + * {@inheritdoc} + */ + public function setRuntimeCacheMaxAge($max_age) { + $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age); + } + } diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index fdec551..61328a2 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -10,7 +10,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Context\CacheContextInterface; -use Drupal\Core\Cache\RuntimeCacheContextDependencyInterface; +use Drupal\Core\Cache\RuntimeCacheableDependencyInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -130,7 +130,7 @@ protected function doGet($name, $immutable = TRUE) { } // Propagate cache contexts to the config object. - $this->cache[$cache_key]->addRuntimeCacheContexts($this->loadCacheContextOverrides()); + $this->propagateCacheableDependencyOverrides($cache_key); return $this->cache[$cache_key]; } @@ -190,8 +190,8 @@ protected function doLoadMultiple(array $names, $immutable = TRUE) { } } - // Propagate cache contexts to the config object. - $this->cache[$cache_key]->addRuntimeCacheContexts($this->loadCacheContextOverrides()); + // Propagate cacheable dependencies to the config object. + $this->propagateCacheableDependencyOverrides($cache_key); $list[$name] = $this->cache[$cache_key]; } @@ -220,17 +220,21 @@ protected function loadOverrides(array $names) { } /** - * Get arbitrary cache context overrides. + * Propagates overridden cacheable dependencies to cached config objects. * - * @return array - * An array of cache context tokens. + * @param string $cache_key + * The key of the cached config object to update. */ - protected function loadCacheContextOverrides() { - $overrides = []; + protected function propagateCacheableDependencyOverrides($cache_key) { + $overrides = ['contexts' => [], 'tags' => [], 'max_age' => Cache::PERMANENT]; foreach ($this->configFactoryOverrides as $override) { - $overrides = Cache::mergeContexts($overrides, $override->getCacheContexts()); + $overrides['contexts'] = Cache::mergeContexts($overrides['contexts'], $override->getCacheContexts()); + $overrides['tags'] = Cache::mergeTags($overrides['tags'], $override->getCacheTags()); + $overrides['max_age'] = Cache::mergeMaxAges($overrides['max_age'], $override->getCacheMaxAge()); } - return $overrides; + $this->cache[$cache_key]->addRuntimeCacheContexts($overrides['contexts']); + $this->cache[$cache_key]->addRuntimeCacheTags($overrides['tags']); + $this->cache[$cache_key]->setRuntimeCacheMaxAge($overrides['max_age']); } /**