Học chút kiến thức DRUPAL 8 và Drupal 9 DEVELOPMENT: CACHING ON

6th Dec 2021
Table of contents

Thông thường, bạn nên tắt tất cả các bộ nhớ đệm khi phát triển Drupal 8. Mặc dù đây là cách dễ nhất để đảm bảo tất cả các thay đổi phát triển được kết hợp khi bạn tải lại trang web, nhưng nó có một số nhược điểm. Toàn bộ trang web trở nên chậm, đôi khi chậm hơn nhiều so với khi bật bộ nhớ đệm. Drupal 8 có một cơ chế bộ nhớ đệm tuyệt vời mà tất cả khách truy cập đều được hưởng lợi, vậy tại sao bạn nên bỏ lỡ với tư cách là nhà phát triển? Phát triển là một cách khác để bạn có thể 'sử dụng' trang web Drupal 8 hoặc Web Drupal 9.

>> Hướng dẫn cài đặt command line cho Drupal 9 - drupal list

>> Hướng dẫn Generating a module and a custom form with Drupal Console

Tôi dành phần lớn thời gian của mình để phát triển Drupal 9 và thực hiện nhiều điều hướng trang và tải lại trên các trang web đã tắt bộ nhớ cache. Bên cạnh đó, đối với một số thứ, bạn cần phải xây dựng lại bộ nhớ cache ngay cả khi cơ chế bộ nhớ đệm bị tắt, chẳng hạn như khi bạn cần làm mới khám phá plugin. Khi nhu cầu này thực hiện các công cụ thông thường là

drush cache-rebuild (drush cr)

drupal cache:rebuild (drupal cr)

Nhược điểm của điều này là nó cũng khởi động drupal - một lần nữa mà không có bộ nhớ đệm - và sau đó xóa tất cả bộ nhớ đệm, mất khá nhiều thời gian để hoàn thành.

Giải pháp cho tất cả những điều này là bắt đầu phát triển với bộ đệm được kích hoạt. Bây giờ tôi phát triển với tất cả các cơ chế bộ nhớ đệm trên: opcache, bộ đệm ẩn động và tĩnh D8, bộ đệm JS và CSS, v.v. Tôi chỉ làm mới phần bộ đệm cần cập nhật. Điều này làm cho trang web của tôi nhanh hơn trong quá trình phát triển, làm cho các lệnh xóa bộ nhớ cache nhanh hơn và giúp tôi hiểu phần nào của trang web được bao phủ bởi cơ chế / bin bộ nhớ cache nào. Tôi có thể giới thiệu nó cho mọi nhà phát triển Drupal 9.

CACHE BIN REFERENCE TABLE

Below is are some handy reference tables that shows the various ways you can selectively clear a cache bins / mechanisms.

When you work on Clear these cache bins / mechanisms Possible methods
Modules bootstrap / discovery bin Admin Toolbar > Flush All Caches > plugins
drush cache-clear module-list
drupal cache:rebuild bootstrap / discovery
sql truncate cache_bootstrap / cache_discovery
Plugins discovery bin Admin Toolbar > Flush All Caches > plugins
drupal cache:rebuild discovery
sql truncate cache_discovery
Routes data bin Admin Toolbar > Flush All Caches > routing and links
drush cache-clear router
drupal cache:rebuild data
drupal router:rebuild
sql truncate cache_data
Menus/Links/Actions/Tasks discovery / menu bin  Admin Toolbar > Flush All Caches > routing and links
drupal cache:rebuild discovery / menu
sql truncate cache_discovery / cache_data
Pages/Blocks render bin Admin Toolbar > Flush All Caches > render
drush cache-clear render
drupal cache:rebuild render
sql truncate cache_render
Content entity / render bin Admin Toolbar > Flush All Caches > render
drush cache-clear entity / render
drupal cache:rebuild entity / render
sql truncate cache_entity / cache_render
Views data / discovery bin drush cache-clear views
drupal cache:rebuild data / discovery
sql truncate cache_data / cache_discovery
Twig templates Twig cache, default bin drush twig-compile
drupal cache:rebuild default
sql truncate cache_default
CSS CSS cache Admin Toolbar > Flush All Caches > CSS and Javascript
drush cache-clear css-js
drush state-delete drupal_css_cache_files / system.css_js_query_string (?)
drupal state:delete drupal_css_cache_files / system.css_js_query_string (?)
JS JS cache Admin Toolbar > Flush All Caches > CSS and Javascript
drush cache-clear css-js
drush state-delete system.theme.files (?)
drupal state:delete system.theme.files (?)
Themes bootstrap / default / discovery bin drush cache-clear theme-registry / theme-list
drush state-delete system.theme.files / system.theme_engine.files (?)
drupal cache:rebuild bootstrap / default / discovery
drupal state:delete system.theme.files / system.theme_engine.files (?)
sql truncate cache_bootstrap / cache_default / cache_discovery

FIND THE RELEVANT CACHE BIN

To know which cache bin affects what is sometimes tricky - you will need to figure this out. Here is my current understanding. If you cannot get a refresh to happen using a specific cache you can always revert to using GUI/drush/drupal cache clear all.

The various cache bins are used for the following:

  • bootstrap
    • Data needed from the beginning to the end of most requests, that has a very strict limit on variations and is invalidated rarely.
    • base system, module system, and theme system information.
  • config
    • configuration (?)
    • config entities (?)
  • container
    • Drupal service container
  • data
    • Contains data that can vary by path or similar context.
    • library_info (CSS, JS)
    • routes
    • route_match
    • views config
  • default
    • arbitrary caches
    • module list
    • twig templates
    • theme registry
    • locale
    • views_data
  • discovery
    • Contains cached discovery data for things such as plugins, views_data, or YAML discovered data such as library info.
    • plugins
  • discovery_migration
  • dynamic_page_cache
  • entity
    • content entity data.
  • menu
    • menu trees
  • migrate
  • render
    • Contains cached HTML strings like cached pages and blocks, can grow to large size.
    • rendered pages
  • toolbar

Then there are some non-cache bin caching mechanisms:

  • JS
    • Contains compacted and combined JS files.
    • query string to refresh browser caches
    • collection_optimizer to combine and compact files
  • CSS
    • Contains compacted and combined CSS files.
    • query string to refresh browser caches
    • collection_optimizer to combine and compact files
  • Twig PHP Storage cache
    • Contains twig templates rendered to PHP files.
  • Drupal static repository
    • Contains static variables set by drupal_static().
  • key-value pairs
    • stored in SQL key_value table
    • Contains State items.

BACKGROUND: FUNCTIONS CALLED

As background to the above I include here a list of functions called by the various commands.

Configuration > Development > Performance > Clear all caches

  • drupal_flush_all_caches();

Configuration > Development > Performance > Save configuration

  • cssCollectionOptimizer->deleteAll();
  • jsCollectionOptimizer->deleteAll();
  • renderCache->deleteAll();

Admin Toolbar > Flush All Caches > 

  • CSS and Javascript (flush browser JS and CSS)
    • reset system.css_js_query_string
  • plugins (part of the discovery bin)
    • \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
  • render (render bin)
    • $this->cacheRender->invalidateAll();
  • routing and links (menu and part of the discovery bin)
    • menu_cache_clear_all(); 
    • $this->menuLinkManager->rebuild();
      • $this->cacheTagsInvalidator->invalidateTags('config.system.menu.*');
    • $this->contextualLinkManager->clearCachedDefinitions(); 
    • $this->localTaskLinkManager->clearCachedDefinitions(); 
    • $this->localActionLinkManager->clearCachedDefinitions();
  • static (resets drupal statics)
    • drupal_static_reset();

drush cache-clear

  • drush
    • drush_cache_clear_drush();
  • theme-registry (part of the bootstrap and part of the default bin)
    • \Drupal::service('theme.registry')->reset();
      • Cache::invalidateTags(['theme_registry']);
  • router (part of the data bin)
    • \Drupal::service('router.builder')->rebuild();
  • css-js
    • _drupal_flush_css_js();
      • reset system.css_js_query_string
    • drupal_clear_css_cache();
      • \Drupal::service('asset.css.collection_optimizer')->deleteAll();
    • drupal_clear_js_cache();
      • \Drupal::service('asset.js.collection_optimizer')->deleteAll();
  • module-list (part of the default bin)
    • \Drupal::cache()->delete('system.module.info');
    • drupal_static_reset('system_get_info');
  • theme-list 
    • \Drupal::service('theme_handler')->rebuildThemeData();
      • $this->state->set('system.theme.files', $files_theme);
        • part of the key_value db table
  • render (render bin)
    • Cache::invalidateTags(['rendered']);
  • views (part of the data and part of the discovery bin)
    • views_invalidate_cache()
      • \Drupal::service('router.builder')->setRebuildNeeded();
      • \Drupal::getContainer()->get('views.route_subscriber')->reset();
      • \Drupal::service('plugin.manager.block')->clearCachedDefinitions();

drush cache-rebuild

  • drupal_rebuild();
  • drush_cache_clear_drush();

drupal cache:rebuild 

  • all
    • drupal_flush_all_caches();
  • bootstrap
  • config
  • container
  • data
  • default
  • discovery
  • discovery_migration
  • dynamic_page_cache
  • entity
  • menu
  • migrate
  • render
  • toolbar
  • static
    • A special cache bin that does not persist beyond the length of the request.

drupal console invokes $cache->deleteAll() on the specified bin.

SQL truncate table 

  • cache_bootstrap
  • cache_config
  • cache_container
  • cache_data
  • cache_default
  • cache_discovery
  • cache_discovery_migration
  • cache_dynamic_page_cache
  • cache_entity
  • cache_menu
  • cache_migrate
  • cache_render
  • cache_toolbar

The cache tables correspond to cache bins.

PHP (Drupal 8 cache API)

  • $cache = \Drupal::cache('bootstrap');
    • use any cache bin name, no parameter returns the 'default' cache.
  • $cache->deleteAll();
    • DatabaseBackend: Run SQL truncate on cache_bin_name table.
  • more granular get, set, invalidate and delete functions available.
    • NB: If you mark cache items as invalid, they may be returned in later calls to get(), if the $allow_invalid argument is TRUE.
  • DefaultPluginManager::clearCachedDefinitions();
    • Cache::invalidateTags($this->cacheTags); or
    • $this->cacheBackend->delete($this->cacheKey);
  • views_invalidate_cache()
    • \Drupal::service('router.builder')->setRebuildNeeded();
    • \Drupal::getContainer()->get('views.route_subscriber')->reset();
    • \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
  • drupal_rebuild()
    • works even when drupal does not bootstrap
    • PhpStorageFactory::get('twig')->deleteAll();
    • $cache->deleteAll();
    • drupal_flush_all_caches();
  • drupal_flush_all_caches()
    • Clears all persistent caches:
      • $cache_backend->deleteAll();
    • Clears asset (JS/CSS) file caches.
      • _drupal_flush_css_js();
      • \Drupal::service('asset.css.collection_optimizer')->deleteAll();
      • \Drupal::service('asset.js.collection_optimizer')->deleteAll();
    • Resets all static variables that have been defined via drupal_static().
      • drupal_static_reset();
    •   Invalidate the container.
      • \Drupal::service('kernel')->invalidateContainer();
    • Wipe the Twig PHP Storage cache.
      • PhpStorageFactory::get('twig')->deleteAll();
    • Updates the system with latest information about extensions (modules and themes).
    • Updates the bootstrap flag for modules implementing bootstrap_hooks().
    • Rebuilds the full database schema information (invoking hook_schema()).
    • Rebuilds data structures of all modules (invoking hook_rebuild()). In core this means
      • blocks, node types, date formats and actions are synchronized with the database
      • The 'active' status of fields is refreshed.
    • Rebuilds the menu router.
      • \Drupal::service('router.builder')->rebuild();
Bạn thấy bài viết này như thế nào?
0 reactions

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