Overview

Views in Drupal 8 automatically adds cache tags to every view so that their content can be invalidated when it changes (and can be cached as long as possible until they do).

However, Drupal 8 only has a single list cache tag for every entity type. Every view that lists nodes is tagged with node_list and will be invalidated when a node is added, changed or deleted.

Views usually have filters. They might list nodes of a specific type, or nodes that are tagged with a certain term and a combination of three other filters. By using more specific cache tags, it is possible to only update views that actually might list the changed node.

This module replaces the hardcoded cache tag with a form that allows developers to set different cache tags based on configuration of the view. It is also possible to set cache tags based on arguments/contextual filters.

Since #2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing (8.9+), it is possible to use the default bundle cache tags (<entity_type>_list:<bundle>, e.g. node_list:article) in views until drupal core supports these automatically in the cache plugin. Alternatively or additionally, custom code can be used similar or more specific cache tags, e.g. based on the promoted or a taxonomy field.

function yourmodule_node_presave(NodeInterface $node) {
  $tags = ['mysite:node:' . $node->getType()];
  if ($node->hasField('field_category')) {
    $tags[] = 'mysite:node:category:' . $node->field_category->target_id;
  }
  // If the category changes, also invalidate the original value.
  if ($node->original && $node->node->original->hasField('field_category')) {
    $tags[] = 'mysite:node:category:' . $node->field_category->target_id;
  }

  Cache::invalidateTags(array_unique($tags));
}

Status

The module is in an early phase and will likely be extended in the future, for example with functionality to invalidate cache tags based on configuration.

Features

  • A views cache plugin that allows to configured the added cache tags
  • Support for setting cache tags based on passed in arguments

Requirements

Views Custom Cache Tags was built for D8. There will be no back port.

Documentation

Further documentation will follow.

Supporting organizations: 
Initiation, Architecture, Implementation, Maintenance

Project information

Releases