Blog

Caching In Drupal 8
15 Jun 2019

Fundamentals Of Caching In Drupal 8

Whether you’re a site developer, module or theme developer, or only an end user of a Drupal website, you’ll find new features in Drupal 8 to give you the liberty to build exceptional digital experiences.

Caching is a popular method to optimize the performance of a website. It is a process that stores web data (HTML, CSS, Images) in some open space. Moreover, a cache has detailed information about the validity of a resource. This information defines how long the resource is valid and should not be considered stale. The data can be stored at every level, right from the primary server, to intermediate proxies, to the user’s browser.

By default, Drupal 8 enables two modules: Internal Page Cache and Internal Dynamic Page Cache.

Internal Page Cache caches pages for anonymous users. Internal Dynamic Page Cache caches contents of the page, except for the personalized pieces, so they can be used for the anonymous and authorized users. Each object of the page contains metadata, and this piece of metadata tells the Internal Dynamic Page Cache module if it has to cache the page or not. You can change default metadata via Cache API.

These modules work in combination with each other as well. If the header is cached by the dynamic page cache, then an anonymous visitor goes to a page on the site, that page will get the cached version of the header, build the remainder of the page on the fly, and then store the whole final result in the internal page cache.

Unless there is a compelling reason not to, these modules should generally both be enabled on almost any Drupal 8 site. One peculiarity is that if all of your user activity happens while they’re logged in, you may wish to consider uninstalling the internal page cache module,as it won’t do much of anything for your site.

All things that are either directly rendered or used to determine what to render, contribute to cacheability metadata. Cacheability metadata consists of three properties:

• Cache tags: If our renderable arrangements depend on some data, such as entity data or some configuration values, we use cache tags to invalidate the data.

• Cache contexts: Cache context is used when our renderable arrays depend on some context, such as user role, theme or URL.

• Cache max-age: Cache controls how long an item may be cached.

Caching allows retrieval without having to request the data from the original source, and it is an essential part of website speed optimization. If you want your users to get pages as quickly as possible, then enable the cache. In Drupal 8, caching has been considerably improved.

Google+