Warm Your WordPress Cache

A few days ago, I updated to the latest version of WordPress, 4.9.4, on most of my WP sites. On a couple of the larger sites, I had been using PHP script to warm the caches to increase page load speeds. I wrote the script about 4 years ago, and it has performed flawlessly up until this latest WP update. I am not sure as to why just yet. I haven’t had the time to really dig in and investigate.

As soon as I realized, though, that the code was broken, my heart sank. Warming the cache on a site with a few thousand pages is not a fun task if you have to do it manually. And, being as I am using the KeyCDN Cache Enabler plugin (awesome little cache plugin by the way.) The PHP script was the only way I had of warming the caches. Well, needless to say, but I was a bit stressed.

I thought about reworking the script to try and get it to work. Then, I thought to myself – surely someone else has written a similar script that is more recent, and thus more apt to work with the newer WP version. Well, it took a while, but I found exactly what I was looking for in the form of a relatively simple bash script. It works really well for warming the cache on even a large WP installation.

I found the script on Github, and it was developed by a really cool guy named Thomas Fritz. To use the script, just copy and paste the following code into a new file, and then name the file Warmly.sh

Upload the file to your base WP directory and ensure that you make it executable.

#!/bin/bash

# warmly.sh
# A wget based, easy, poor man`s cache warmer script
# https://gist.github.com/thomasfr/7926314

# The MIT License (MIT)
#
# Copyright (c) 2013,2014 Thomas Fritz  (http://fritzthomas.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


CURRENT_WORKING_DIR="$(pwd)"
WARMLY_HOME="$(cd $(dirname $0);pwd)"
cd $CURRNT_WORKING_DIR

WARMLY_DEFAULT_USER_AGENT="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; WARMLY)"
WARMLY_USER_AGENT="${2:-$WARMLY_DEFAULT_USER_AGENT}"
WARMLY_START_URL="${1}"
WARMLY_WAIT_NEXT_RUN="600s"
WARMLY_WGET_LOGFILE="${WARMLY_HOME}/wget.log"
WARMLY_WAIT_SECONDS_BETWEEN_REQUESTS="0.5"
WARMLY_REJECT="gif,jpg,jpeg,png,pdf,txt,xml,ico,svg,css,js,json"
WARMLY_RUN=1

if [ -z $WARMLY_START_URL ]; then
	echo "No starting url set"
	exit 1
fi

echo "Using User-Agent: '$WARMLY_USER_AGENT'"
echo "Warmly starts here: '$WARMLY_START_URL'"

echo "$(date) START"
while :
	do
		echo "$(date) START RUN '#${WARMLY_RUN}'"
		wget --no-cache --reject=${WARMLY_REJECT} --wait=${WARMLY_WAIT_SECONDS_BETWEEN_REQUESTS} --limit-rate=400k --delete-after --recursive --no-directories --timestamping --level=3 --tries=3 --timeout=20 --no-verbose --output-file="${WARMLY_WGET_LOGFILE}" --user-agent="${WARMLY_USER_AGENT}" "${WARMLY_START_URL}"
		if [ $? -gt 0 ] ; then
			exit 1;
		fi
		echo "$(date) WAIT '${WARMLY_WAIT_NEXT_RUN}' for next run"
		sleep "${WARMLY_WAIT_NEXT_RUN}"
		let "WARMLY_RUN=WARMLY_RUN+1"
done

Then, just log in with your terminal application, switch to the WordPress base directory, and then type the following at the command line:

./warmly.sh yoursite.com

Just make sure to replace “yoursite.com” with your site’s homepage URL.

That’s it. Once you type in the command, the script will run and proceed to cache all the pages on your site. Try it out and comment below to let me know how it goes.

Until next time..

Jeff

5 thoughts on “Warm Your WordPress Cache”

  1. Hi Jeff, thanks for the code and guide. I just have a few questions.
    1. How deep does it crawl? Will it crawl every page or only certain levels deep from the homepage?
    2. Will the script continually run in the background or will it need to be started every time it has finished and the cache is purged?
    2. Is it possible to trigger the script using a cronjob, and if so how?

    Thanks

    1. Hi John. Sorry for the late reply. It will run continuously through all levels of pages and posts, as long as the hierarchy is not broken. It will run once through all pages it encounters and then end. If you clear the cache, you will need to run the script again. Sure, I guess you could run this would a cron job. Would just need to set up the bash script to run when needed. You might even be able to do it with a WP Cron plugin. although I haven’t tried it. Hope this helps

  2. Thanks again Jeff. My goal with the script is to help speed up the mobile experience. I’m currently running AMP with the WP AMP plugin and redirecting all mobile users to the AMP version.Uncached pages are a tad slower so I modified the script so that the user agent is a mobile one.

    I’m using Litespeed and the warm cache plugin mentioned below didn’t seem to cache pages with Litespeed like it did with WP Rocket. I hope your script works. I’ve just started a run now, so fingers crossed.

    I’ll try to wrap my head around the cron job. If I can set it up for 2 or 3 times a day it should keep the site running nice and fast.

Join the Discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.