Load Page Cache after cron Runs

Last updated on
10 January 2020

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Drupal creates pages and serves them to clients. The completed pages are also stored in the cache_page table of the database -- referred to as the Page Cache. This function is enabled on page admin/settings/performance. The nominal settings for the Page Cache section is:

  • Caching Mode: Normal
  • Minimum Cache Lifetime: None
  • Page Compression: Enabled

With these settings the Page Cache will be emptied every time cron runs. Please take this into consideration when you set the number of times cron runs each day. The cron job of our production website runs once per day just after midnight when traffic on the website is low.

The problem with this setup is the Page Cache is empty after cron runs. This means the first time a user accesses a page the completed page will not be in the Page Cache and the access will be slow. We can solve this problem by loading all the webpages of the site when cron completes (known as "cache warming"). You can do this by creating a script that runs the standard cron job followed by some code that loads each page in sitemap.xml into the Page Cache:

#!/bin/bash
#
# this is the standard Drupal cron invocation that runs from cron
#     this will flush the Page Cache when Minimum Cache Lifetime set to <none>
#
wget -O - -q -t 1 http://www.example.com/cron.php
#
# use the sitemap and reload the Page Cache by accessing each page once
#
wget --quiet http://www.example.com/sitemap.xml --output-document - | egrep -o "http://www.example.com[^<]+" | wget -q --delete-after -i -
 

You need to change www.example.com in the code above to the domain of your website. Run this script from cron instead of the standard cron invocation. Now the webpages of your website in sitemap.xml are loaded into your Page Cache waiting for the first user access. This first user access will now be served from the Page Cache -- much faster.

This can also be done with the Cache Warmer module. It's run as a Drush command, and it takes a lot of options.

See also:

Help improve this page

Page status: No known problems

You can: