.../Plugin/views/argument_default/CurrentUser.php | 4 +--- .../user/src/Plugin/views/argument_default/User.php | 4 +--- .../views/config/schema/views.data_types.schema.yml | 6 +++--- core/modules/views/src/Entity/View.php | 21 ++++++++++++++------- .../views/src/Plugin/CacheablePluginInterface.php | 4 ++-- .../Plugin/views/argument/ArgumentPluginBase.php | 4 ++-- .../src/Plugin/views/cache/CachePluginBase.php | 12 ++++++------ .../src/Plugin/views/display/DisplayPluginBase.php | 20 ++++++++++---------- .../src/Plugin/views/filter/FilterPluginBase.php | 14 +++++++------- core/modules/views/src/Plugin/views/sort/Random.php | 8 ++++---- .../views/src/Plugin/views/sort/SortPluginBase.php | 2 +- 11 files changed, 51 insertions(+), 48 deletions(-) diff --git a/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php b/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php index ae0224b..1d49666 100644 --- a/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php +++ b/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php @@ -37,9 +37,7 @@ public function isCacheable() { * {@inheritdoc} */ public function getCacheContexts() { - $contexts = ['cache.context.user']; - - return $contexts; + return ['cache.context.user']; } } diff --git a/core/modules/user/src/Plugin/views/argument_default/User.php b/core/modules/user/src/Plugin/views/argument_default/User.php index 9084b38..ecf09ca 100644 --- a/core/modules/user/src/Plugin/views/argument_default/User.php +++ b/core/modules/user/src/Plugin/views/argument_default/User.php @@ -86,9 +86,7 @@ public function isCacheable() { * {@inheritdoc} */ public function getCacheContexts() { - $contexts = ['cache.context.url']; - - return $contexts; + return ['cache.context.url']; } } diff --git a/core/modules/views/config/schema/views.data_types.schema.yml b/core/modules/views/config/schema/views.data_types.schema.yml index 2539ea6..13b1c04 100644 --- a/core/modules/views/config/schema/views.data_types.schema.yml +++ b/core/modules/views/config/schema/views.data_types.schema.yml @@ -268,15 +268,15 @@ views_display: field_langcode_add_to_query: type: string label: 'Add the field language to the query' - cache_information: + cacheability: type: mapping mapping: cacheable: type: boolean - label: "Cacheable" + label: 'Cacheable' contexts: type: sequence - label: Cache contexts" + label: 'Cache contexts' views_sort: type: views_handler diff --git a/core/modules/views/src/Entity/View.php b/core/modules/views/src/Entity/View.php index 35fa827..5d01d25 100644 --- a/core/modules/views/src/Entity/View.php +++ b/core/modules/views/src/Entity/View.php @@ -303,16 +303,23 @@ public function preSave(EntityStorageInterface $storage) { // @todo Check whether isSyncing is needed. if (!$this->isSyncing()) { - $this->fillCacheMetadata(); + $this->setCacheability(); } } /** - * Fill in all the cache metadata (cache contexts as cacheablity). + * Sets the cacheability of this view. * - * The cache information is stored per view and per display. + * Cacheability is set per view and per display, and ends up being stored in + * the view's config entity. This allows Views to determine very efficiently: + * - whether a view is cacheable at all + * - what the cache key for a given view should be + * + * In other words: this allows us to do the (expensive) work of initializing + * Views plugins and handlers to determine their effect on the cacheability of + * a view at save time rather than at runtime. */ - protected function fillCacheMetadata() { + protected function setCacheability() { $executable = $this->getExecutable(); $current_display = $executable->current_display; @@ -320,11 +327,11 @@ protected function fillCacheMetadata() { foreach ($displays as $display_id => $display) { $executable->setDisplay($display_id); - list($display['cache_information']['contexts'], $display['cache_information']['cacheable']) = $executable->getDisplay()->getCacheMetadata(); + list($display['cacheability']['contexts'], $display['cacheability']['cacheable']) = $executable->getDisplay()->calculateCacheability(); // Always include at least the language context as there will be most // probable translatable strings in the view output. - $display['cache_information']['contexts'][] = 'cache.context.language'; - $display['cache_information']['contexts'] = array_unique($display['cache_information']['contexts']); + $display['cacheability']['contexts'][] = 'cache.context.language'; + $display['cacheability']['contexts'] = array_unique($display['cacheability']['contexts']); } // Restore the previous active display. $executable->setDisplay($current_display); diff --git a/core/modules/views/src/Plugin/CacheablePluginInterface.php b/core/modules/views/src/Plugin/CacheablePluginInterface.php index 9cccb56..a8a72a9 100644 --- a/core/modules/views/src/Plugin/CacheablePluginInterface.php +++ b/core/modules/views/src/Plugin/CacheablePluginInterface.php @@ -8,12 +8,12 @@ namespace Drupal\views\Plugin; /** - * Provides information whether and how the specific views plugin is cacheable. + * Provides information whether and how the specific Views plugin is cacheable. */ interface CacheablePluginInterface { /** - * Returns TRUE if this filter is cacheable at all. + * Returns TRUE if this plugin is cacheable at all. * * @return bool */ diff --git a/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php b/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php index 9b0285b..bf91e3f 100644 --- a/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php +++ b/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php @@ -1180,7 +1180,7 @@ protected function unpackArgumentValue($force_int = FALSE) { public function isCacheable() { $result = TRUE; - // Asks all sub plugins (argument defaults, argument validator and styles). + // Asks all subplugins (argument defaults, argument validator and styles). if (($plugin = $this->getPlugin('argument_default')) && $plugin instanceof CacheablePluginInterface) { $result &= $plugin->isCacheable(); } @@ -1207,7 +1207,7 @@ public function getCacheContexts() { // the information from there. $contexts[] = 'cache.context.url'; - // Asks all sub plugins (argument defaults, argument validator and styles). + // Asks all subplugins (argument defaults, argument validator and styles). if (($plugin = $this->getPlugin('argument_default')) && $plugin instanceof CacheablePluginInterface) { $contexts = array_merge($plugin->getCacheContexts(), $contexts); } diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php index 95e5a29..ec64aa6 100644 --- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php +++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php @@ -368,14 +368,14 @@ protected function getCacheTags() { } /** - * Change the determined cache metadata on saving of the view. + * Alters the cacheability of a display upon saving a view. * - * @param array $cache_contexts - * An array of cache contexts the view output varies by. - * @param bool $cacheable - * Is the display actually cacheable. + * @param bool $is_cacheable + * Whether the display is cacheable. + * @param string[] $cache_contexts + * The cache contexts the display varies by. */ - public function alterCacheMetadata(array &$cache_contexts, &$cacheable) { + public function alterCacheMetadata(array &$cache_contexts, &$is_cacheable) { } } diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index d937be5..d185f3d 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -2288,15 +2288,15 @@ public function preExecute() { } /** - * Determines the available cache metadata from each handler/plugin. + * Calculates the display's cacheability, by inspecting each handler/plugin. * * @return array * Returns an array: - * - first value: An array of cache contexts the view output varies by. - * - second value: (boolean) Is the display actually cacheable. + * - first value: (boolean) Whether the display is cacheable. + * - second value: (string[]) The cache contexts the display varies by. */ - public function getCacheMetadata () { - $cacheable = TRUE; + public function calculateCacheability () { + $is_cacheable = TRUE; $cache_contexts = []; // Iterate over ordinary views plugins. @@ -2304,10 +2304,10 @@ public function getCacheMetadata () { $plugin = $this->getPlugin($plugin_type); if ($plugin instanceof CacheablePluginInterface) { $cache_contexts = array_merge($cache_contexts, $plugin->getCacheContexts()); - $cacheable &= $plugin->isCacheable(); + $is_cacheable &= $plugin->isCacheable(); } else { - $cacheable = FALSE; + $is_cacheable = FALSE; } } @@ -2318,17 +2318,17 @@ public function getCacheMetadata () { foreach ($handlers as $handler) { if ($handler instanceof CacheablePluginInterface) { $cache_contexts = array_merge($cache_contexts, $handler->getCacheContexts()); - $cacheable &= $handler->isCacheable(); + $is_cacheable &= $handler->isCacheable(); } } } /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */ if ($cache_plugin = $this->getPlugin('cache')) { - $cache_plugin->alterCacheMetadata($cache_contexts, $cacheable); + $cache_plugin->alterCacheMetadata($is_cacheable, $cache_contexts); } - return [$cache_contexts, $cacheable]; + return [$is_cacheable, $cache_contexts]; } /** diff --git a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php index 25b4ce3..e804462 100644 --- a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php @@ -1471,6 +1471,13 @@ protected static function arrayFilterZero($var) { /** * {@inheritdoc} */ + public function isCacheable() { + return TRUE; + } + + /** + * {@inheritdoc} + */ public function getCacheContexts() { $cache_contexts = []; // An exposed filter allows the user to change a view's filters. They accept @@ -1482,13 +1489,6 @@ public function getCacheContexts() { return $cache_contexts; } - /** - * {@inheritdoc} - */ - public function isCacheable() { - return TRUE; - } - } /** diff --git a/core/modules/views/src/Plugin/views/sort/Random.php b/core/modules/views/src/Plugin/views/sort/Random.php index 9501f79..1cdfc03 100644 --- a/core/modules/views/src/Plugin/views/sort/Random.php +++ b/core/modules/views/src/Plugin/views/sort/Random.php @@ -36,15 +36,15 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function getCacheContexts() { - return []; + public function isCacheable() { + return FALSE; } /** * {@inheritdoc} */ - public function isCacheable() { - return FALSE; + public function getCacheContexts() { + return []; } } diff --git a/core/modules/views/src/Plugin/views/sort/SortPluginBase.php b/core/modules/views/src/Plugin/views/sort/SortPluginBase.php index c54da35..1491336 100644 --- a/core/modules/views/src/Plugin/views/sort/SortPluginBase.php +++ b/core/modules/views/src/Plugin/views/sort/SortPluginBase.php @@ -239,7 +239,7 @@ public function isCacheable() { */ public function getCacheContexts() { $cache_contexts = []; - // Exposed sorts on the GET parameter, so it depends on the current URL. + // Exposed sorts use GET parameters, so it depends on the current URL. if ($this->isExposed()) { $cache_contexts[] = 'cache.context.url'; }