Tập tành chọt Cache Context - Cache Tags - Disabled Cache

8th Jul 2022
Table of contents

The following examples will use a custom block to show how the caching works in Drupal 8 and 9, but you can use the same thing for any rendered array.

You can disable cache for a custom block in two different ways. You can either use the UncacheableDependencyTrait trait:

<?php

namespace Drupal\test_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\UncacheableDependencyTrait;

/**
 * Provides a 'Test block' Block.
 *
 * @Block(
 *   id = "test_block",
 *   admin_label = @Translation("Test block"),
 *   category = @Translation("Test block"),
 * )
 */
class TestBlock extends BlockBase {

  use UncacheableDependencyTrait;

  public function build() {
    return [
      '#markup' => $this->t('Time is: ') . time(),
    ];
  }

}

or you can set the cache max-age like this:

<?php

namespace Drupal\test_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Test block' Block.
 *
 * @Block(
 *   id = "test_block",
 *   admin_label = @Translation("Test block"),
 *   category = @Translation("Test block"),
 * )
 */
class TestBlock extends BlockBase {

  public function build() {
    return [
      '#markup' => $this->t('Time is: ') . time(),
      '#cache' => [
        'max-age' => 0,
      ]
    ];
  }

}

If you don't want to completely disable the cache for a block, you can use the cache contexts. For example, you can use the user cache context. By using this cache context you will cache the block for each user. This makes sense if the block content is unique to each user. You can see the full list of available cache contexts here.

<?php

namespace Drupal\test_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Test block' Block.
 *
 * @Block(
 *   id = "test_block",
 *   admin_label = @Translation("Test block"),
 *   category = @Translation("Test block"),
 * )
 */
class TestBlock extends BlockBase {

  public function build() {
    $email = \Drupal::currentUser()->getEmail();

    return [
      '#markup' => $this->t('Your email is: ') . $email,
      '#cache' => [
        'contexts' => [
          'user',
        ],
      ]
    ];
  }

}

And the last thing that I want to show you are cache tags. Let's say that a block's content depends on the specific node. You want to make sure that each time the node is updated, that block cache will be invalidated. You can do that like this:

<?php

namespace Drupal\test_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Test block' Block.
 *
 * @Block(
 *   id = "test_block",
 *   admin_label = @Translation("Test block"),
 *   category = @Translation("Test block"),
 * )
 */
class TestBlock extends BlockBase {

  public function build() {
    $node = \Drupal\node\Entity\Node::load(1);
    $node_title = $node->getTitle();

    return [
      '#markup' => $this->t('The title of node #1 is: ') . $node_title,
      '#cache' => [
        'tags' => ['node:1'],
      ]
    ];
  }

}

IMPORTANT NOTE: all this applies only to logged-in users! Next time we'll see how caching works for anonymous users.

Bạn thấy bài viết này như thế nào?
1 reaction

Add new comment

Image CAPTCHA
Enter the characters shown in the image.
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”

Related Articles

Master list (in progress) of how to get parts of fields for use in Twig templates. I’m always having to look these up, so I thought I’d hash them out and write them down.

Litespeed Cache là plugin WordPress dùng để kết hợp với Web Server LiteSpeed nhằm tăng tốc website WordPress của bạn gấp nhiều lần

In this article, we are going to see how some tools & libraries will make people's lives easier during the development & code review process.

In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git

Trước khi tìm hiểu xem PHP Code Sniffer là gì thì các bạn cần phải nắm được coding convention là gì đã.