Drupal's caching mechanism

by Gisle Hannemyr

This chapter is intended to give you a basic understanding of the Drupal cache.

Table of contents

Drupal extensions discussed in this chapter: Devel.

Introduction

Drupal's caching mechanism (resident in the core System module) stores dynamically generated web pages in a cache in the database and reuses them. The pages on your site, rather than being fixed forever, will use ever-changing elements (e.g. text and images) pulled together from various parts of your database. Such pages are said to be dynamically generated.

By caching such pages, Drupal keeps them ready to be used again, instead of having to re-create them each time someone wants to view them. This way, displaying a page composed of several elements takes only one database query instead of several. Queries take time and use CPU, so caching lightens the load on the system and lets it respond more quickly.

technicalTo reduce server load and save bandwidth, Drupal stores and sends cached pages compressed.

noteThe official Drupal documentation says that contents served to the anonymous user are always cached in the internal database cache. It is less clear whether, and to what extent, contents served to authenticated users are cached. I've learnt from experience that even when I am logged in, changes I've made to blocks and theming elements will not appear on the screen until I do a cache clear.

See alsoFor more about Drupal's caching mechanism, see DO: Concept: Cache, DO: Clearing the Cache.

Cache settings

Drupal 9 only let you configure the following cache setting:

To change it. navigate to Manage » Configure » Development » Performance.

This setting controls what is sent as a the maximum age value in a Cache-Control HTTP header field. The default is "<no caching>".

It advises external browsers and proxy servers how long they may serve the page without asking your Drupal website for a new copy. This does not mean that the page will be regenerated by Drupal after this much time. It just means that the proxy server must check back with Drupal to see if a new version of the page exists after this much time. The setting has no impact on when Drupal's own cache in the database gets rebuilt.

What value to use for production depends on your site. If your site rarely change, you can set maximum age to a long time to prevent too frequent hits on your site from external consumers. If your site changes constantly, you may need to go with the default.

Clearing or rebuilding the cache

Cached pages kept in the Druapl database are automatically expired during cron runs, and rebuilt the next time they're requested.

You can force a rebuild of the cache from the CLI with this command:

$ drush cr

In the GUI, you may navigate to Manage » Configure » Development » Performance and press "Clear all caches" to force clearing all caches.

If you need a slightly quicker way to do this, install the Devel project, you can navigate to Devel » Cache clear to have access to forcing a cache clear from the GUI.

To force cache clear of the browser cache, hold down the Shift key while refreshing the page.

Final word

[TBA]


Last update: 2022-08-25 [gh].