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

Recently I found myself needing to manage cookie data in Drupal 9 for the first time. As oft, my first step in the process was to search through Drupal core and the Drupal API for examples of “the right way” to handle cookies. After I didn’t find any reasonable existing solution within Drupal, it was time to solve this on my own.

Regardless of your position on either side of the web, there is always risk of being a victim of a cyber attack. This has put businesses in a tizz to make personal data of the visitors secure.

we had 5 domain on our website and we need to redirect a user to US domain if user is browsing from US

Migration from one version of Drupal to the other version of Drupal has always been important as it helps to move with time and stay updated with the latest technology.

in this article, I'll show you how to create custom controller with JSON response in Drupal 9