Tắt tính năng bộ nhớ đệm (caching) Drupal 8/9 trong quá trình development

9th Jul 2022
Tắt tính năng bộ nhớ đệm (caching) Drupal 8/9 trong quá trình development
Table of contents

Đối với Drupal 8, trong quá trình bạn phát triển (development) cho module hay theme, để những dòng code của bạn được thực thi khi tải lại trang, bạn cần phải xoá bộ nhớ đệm (clear cache) để xoá cache render, cache động hay cache twig.

Nếu như bạn đã đọc qua bài viết Những lệnh drush cơ bản, bạn có thể dùng lệnh drush cr. Tuy nhiên, dù bạn sử dụng drush để clear cache thì việc này vẫn khiến bạn mất khá nhiều thời gian. Chính vì vậy, bạn cần tắt tính năng cache trong quá trình development.

What would happen if we have a high traffic web site with hundred of different node bundles and hundred of views displaying different kind of nodes
What would happen if we have a high traffic web site with hundred of different node bundles and hundred of views displaying different kind of nodes

Mình sẽ hướng dẫn bạn 2 cách sau:

1. CẤU HÌNH TÍNH NĂNG LOCAL DEVELOPMENT

1.1. Sao chép tập tin example.settings.local.php trong thư mục sites rồi đổi tên thành settings.local.php và đặt trong đường dẫn sites/default

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

1.2. Sửa tập tin settings.php trong sites/default rồi bỏ chú thích (uncomment) dòng code sau (ở gần cuối tập tin, phía trên phần cấu hình cơ sở dữ liệu):

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

1.3. Kiểm tra tập tin settings.local.php xem đã bỏ chú thích dòng code này chưa:

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

Nếu chưa thì hãy bỏ chú thích.

Mặc định trong tập tin development.service.yml chứa dòng cấu hình tắt tính năng Drupal cache:

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

1.4. Để bật tính năng css and js aggregation, cài đặt những tham số sau bằng TRUE trong tập tin settings.local.php:

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

1.5. Để tắt cache render, bỏ chú thích dòng lệnh sau trong tập tin settings.local.php:

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

1.6. Để tắt cache trang động (dynamic page cache), bỏ chú tích dòng lệnh sau trong tập tin settings.local.php:

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

1.7. Để tắt cache trang nội bộ (internal page cache), trong tập tin settings.local.php, bỏ chú thích dòng lệnh sau:

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

1.8. Nếu bạn có nhu cầu development module hoặc theme, hãy bỏ chú thích và cài đặt giá trị bằng FALSE cho dòng lệnh sau trong tập tin settings.local.php

$settings['extension_discovery_scan_tests'] = FALSE;

1.9. Để tắt tính năng cache twig, hãy thêm những dòng lệnh sau vào tập tin development.services.yml ở thư mục sites:

parameters:
  twig.config:
    debug: true
    auto_reload: true
    cache: false

Cuối cùng, tập tin development.services.yml sẽ giống như sau:

# 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

1.10. Để chắc chắn cho việc cập nhật những cấu hình trên, bạn hãy dùng lệnh cache rebuild:

drush cr

2. SỬ DỤNG DRUPAL CONSOLE

Chỉ cần gõ lệnh sau:

drupal site:mode dev

Muốn trở lại như cũ, gõ lệnh

drupal site:mode prod
Bạn thấy bài viết này như thế nào?
2 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.

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ì đã.