How does caching work in Drupal?
In my experience, there seems to be a lot of misunderstanding how Drupal caching works. I'm not talking about advanced caching mechanisms like Varnish, APC or memcache here, I'm talking about the core Drupal caching: page cache and block cache which are available in Drupal 7 core. Drupal 6 is similar but works a but differently because page caching was simplified for Drupal 7 (e.g. aggresive mode is now a available as a settings.php var) and the entire system also has become pluggable.
Everybody knows this screen and the settings. But what do they exactly mean? What difference do they actually make? What do they do? Here is an attempt to explain the settings at '/admin/config/development/performance'.
- If enables, page caching will only be used for anonymous users.
- If you enable page caching, the entire HTML of each page will be stored in the database. This significantly reduces the amount of queries needed.
- This automatically means that blocks will also get cached, disregarded their own cache settings.
- If Varnish is configured, this will also enable varnish caching
- The page cache max-age will be used (see below) for each page.
- You can check the "X-Drupal-Cache" HTTP header to check if this cache is HIT.
- If you enable block caching, without enabling page caching, the block cache settings will be used (for anonymous and authenticated).
- The defaults for most blocks is "don't cache" because that is the default for blocks who don't have their cache settings specifically declared.
- You can use block_cache_alter module to set this for each block.
- When it's block created by Views, you can set the cache settings for a block in Views.
Minimum cache lifetime:
- This is the amount of time before the page cache is cleared.
- Page caches are cleared on each cron run (system_cron).
- Be warned: incorrect settings of your cron and this setting might kill your website performance. See also this issue.
Expiration of cached pages:
- This only applies for external caching mechanisms, for example your browser cache or Varnish.
- It sets the Cache-Control max-age value in the HTTP-HEADERS.
- This setting only takes effect when page caching is turned on.
If I'm wrong at any of these points - or am missing something, please let me know and I will update this page.
Also, check out chapter 30 of "The definitive guide to Drupal 7" for a complete layout of the enitire page load process in Drupal.
Photo credits: Question mark sign by Colin Kinner © 2008 shared under a Creative Common license.