Skip to main content

[Thủ Thuật Drupal] Disable Cache trong Drupal 8/ Drupal 9

Drupal có một hệ thống cache vô cùng mạnh mẽ và chia ra rất nhiều loại khác nhau : Dynamic caching, Page caching, Twig template caching, ... Nhưng đôi khi chúng ta cần phải tắt đi để thực hiện một số thay đổi hoặc vì phát triển hệ thống. Dưới đây sẽ là hướng dẫn xáo cache trong Drupal 8/ Drupal 9.

Bước 1 - Copy file example.settings.local.php trong folder sites đến sites/default 

cp sites/example.settings.local.php sites/default/settings.local.php

Bước 2 - Xóa comment trong đoạn dưới trong settings.php

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

Bước 3 - Xóa comment trong file settings.local.php

$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

Trong mặc định, trong file development.service.yml đã có đoạn tắt Drupal cache:

parameters:
  http.response.debug_cacheability_headers: true
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

Bước 4 - Setting lại những tham số bạn cần trong settings.local.php

Nếu muốn css and js được tổng hợp lại thì để TRUE, còn khi thì FALSE

$config['system.performance']['css']['preprocess'] = TRUE;
$config['system.performance']['js']['preprocess'] = TRUE;

Tắt tạo cache bằng cách bỏ comment

$settings['cache']['bins']['render'] = 'cache.backend.null';

Bỏ comment để tắt cache cho trang động (Dynamic Page)

$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

Tắt cache trong trang nội bộ (internal page)

#bỏ comment
$settings['cache']['bins']['page'] = 'cache.backend.null';

Nếu bạn đang phát triển theme/ module bạn có thể set bằng False

$settings['extension_discovery_scan_tests'] = FALSE;

Bước 5 - Tắt Cache cho Twig

Thêm đoạn code bên dưới vào file development.services.yml trong folder sites, như dưới đây

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

Bước 6 - Vào trang Admin, xóa cache và kiểm tra