Drupal 8 Tags based cache system

Submitted by editor on Tue, 06/07/2016 - 13:52
Question

How to use the tags based cache system of drupal 8 ?

This system allow you to invalidate drupal cache using a 'tag'

Exapples:

\Drupal\Core\Cache\Cache::invalidateTags(array ('config:views.view.front'));
\Drupal\Core\Cache\Cache::invalidateTags(array ('config:system.performance'));
\Drupal\Core\Cache\Cache::invalidateTags(array ('user:1'));
\Drupal\Core\Cache\Cache::invalidateTags(array ('node:1'));

Tags Syntax

A cache tag is a string like thing:identifier

Examples:

node:1 - cache tag for Node entity 1 (invalidated whenever it changes)
user:1 - cache tag for User entity 1 (invalidated whenever it changes)
config:system.performance -  cache tag for the system.performance configuration
config:views.view.front - cache tag for the front view configuration
And also:
library_info - cache tag for asset libraries
route_match - cache tag for a path (Route)
node_list - list cache tag for Node entities (invalidated whenever any Node entity is updated, deleted or created, i.e. when a listing of nodes may need to change)

How to find the tag names ?

You can yse the ::getCacheTags() method of Entity based classes to know tag names.

For the Views:

$tags = \Drupal\views\Entity\View::load('front')->getCacheTags();

For the users:

$tags = \Drupal\views\Entity\View::load('news')->getCacheTags();

 

Cache by route name (route_match)

Some datas are cached using the URL/Path and the tag 'route_match'. Fot that, invalidate cache bu CID (cache ID)
Example:

route_match
Page : /admin
cid  : route:/admin:
tags : route_match

For a custom Page , View, Node ...
tags : route_match
cid  :
route:/node/10:
route:/node/10:_wrapper_format=drupal_ajax

tags : route_match
cid  :
route:/thapathato/myview:
route:/thapathato/myview:_wrapper_format=drupal_ajax

 

Link : https://www.drupal.org/developing/api/8/cache/tags

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.