Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Drupal 8 Tags based cache system

By editor , 7 June, 2016
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

Tags

  • Drupal 8
  • Code
  • Cache
  • Module
Create a simple module for Drupal 8 - Step 3 - Create Dynamic Page/ Block
Drupal 8 Cache API
Cache Drupal 8 Render Arrays
Drupal 8 Cache contexts
Cache Clearing and rebuilding methods for Drupal 8

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer