An In-Depth Guide to Effective WordPress Caching
- 1 Comment
A while ago, I wrote an article on caching — What Is Website Caching and Why Is It So Important?, that looked at how caching speeds up websites by serving static content whenever possible. In this next article on caching, I’ll be focusing on WordPress specifics, and what you can do as a website owner to leverage caching as effectively as possible.
In a nutshell, every time your site loads it performs scores of database queries. Caching saves a static version of your page and sends that to each user — sidestepping the database altogether.
I wrote a lengthy section on the intricacies of the caching process, but I realized it wasn’t really necessary. Creating the cached version of a page is trivial — knowing when to purge the cache is the real challenge.
Clearing the Cache
This refers to the process of deleting the static copy of the page. If there’s no cached version, all the database queries run as usual, and the result is saved again, creating a new static copy.
The content of your homepage is more or less constant; it only changes when you publish a new post. A good caching system will serve a static version, purging the cache only when it detects the publishing event. It will then continue to serve the new static version until you publish another post.
Effective WordPress Caching
Caching is effective when it keeps your pages current with the minimal number of cache clearing operations.
You could create a cached version of your front page that expires every second. It would always be current, but would impact performance negatively:
- Some 99.9999% of cache deletions would be unnecessary.
- The cache would be regenerated every second, which would lead to more database queries being performed than required.
You could also set your front page cache to expire every week. The result would be a hundred database queries every week instead of every second, but your homepage would rarely show its most current form.
How about choosing something in between, such as 24 hours? Still very few queries would be needed, but your homepage would still be out of sync with your content for up to 24 hours.
For some rarely visited or non-time-sensitive pages this could be just fine, but most of the time it’s more important for your front page to stay current than to conserve resources.
Because of the nature of time-based cache clearing, action-based methods are better in most cases. WordPress contains a bunch of actions that it fires when certain conditions are met.
One such example is the post publish event. Caching plugins can detect when a post is published, and then delete the necessary pages.
This method is perfect, because pages remain static for as long as possible and are cleared exactly when needed.
Manual Cache Clearing
It’s also possible to clear caches manually. This is useful when you need to clear your cache, but no automatic event has been fired.
WordPress Cache Settings
Luckily, you don’t need to worry too much about clearing the cache — plugins will figure it all out for you. Let’s get started by going through the WP Super Cache settings with my recommendations and notes.
Once the plugin is installed, head on over to Settings->WP Super Cache->Advanced.
In the first group you should, of course, turn caching on. In the second group, select use mod_rewrite initially, then go to the Easy settings tab and use the cache tester to see if it works.
If it doesn’t, you can try enabling mod_rewrite (it will require some sysadmin experience), or you can go for the PHP caching setting.
I recommend ticking the first two settings: Compression and not caching for known users. You may want to ask your host if they have any issues with compression, and, if they do, or if you see any problems, make sure to disable it.
I always disable caching for known users (logged in users) because it allows me to log in and see the uncached (most recent) version of the site. This can be helpful during development when changes are pushed to the theme, but the cache isn’t cleared, since no content changes have been made.
Note that if you have a site that operates with a lot of logged in users — such as a subscription-based site — you may want to leave this switched off. If this is the case, I recommend ticking the ‘Make known users anonymous’ setting as well. The only exception is if your logged in users edit content.
Keep the cache rebuild option checked, as this will optimize the process of generating a new cached file.
The ‘Don’t cache pages with GET parameters’ setting depends on how your site is built. GET parameters are additional bits tacked on to the end of the URL, and if you use them for manipulating content on a page, as utm sources for tracking, or anything similar, I recommend ticking the box.
For some scenarios, such as utm parameters, you can turn them into anchor tags. See Joost’s urm source comment for more information.
If you want to serve cached pages to mobile device users, you’ll need to check the mobile device support option and use a recommended plugin — or your own developer. The recommended plugins are:
‘Clear all cache files when a post or page is published or updated’ should not be checked for most users. However, if you have a bunch of recommended related items littered around your website and/or changes in a single post affect a number of systems on other pages, you may want to try clicking this option.
Extra homepage checks should be turned on. It may cause some unnecessary cache clearing for the homepage, but it’s more important to stay current there — the overhead is warranted.
If you’re using the default WordPress commenting system, you should tick the ‘Only refresh current page when comments made’ option. This will make sure comments show up as soon as they’re approved.
Expiry Time & Garbage Collection
This section dictates how and when old cache files are removed. When a new cached version is created, the old one isn’t removed — the new version is referred to as fresh, the old one is referred to as stale, and is kept around.
I recommend reading the documentation under these settings for some tips on how to set the values.
Accepted Filenames & Rejected URIs
This section lets you exclude some content types from the cache altogether, and I recommend ticking the search page here to make sure results are always fresh.
The textbox below allows you to selectively remove some paths from caching, which can be useful for additional search pages or pages you’re developing.
Lock down mode will make sure comments do not trigger cache clearing. This will result in comments showing up with a little lag, but may keep your site online during a traffic spike.
There are a bunch of other plugins out there that provide similar functionality. The most notable are:
Their settings will be slightly different, but the thinking behind configuring them is the same. You need to make some decisions about your website and strike a balance between always being fresh and how much resource is conserved.
Managed WordPress Hosting
Caching plugins for WordPress are great, but they operate on the same level as WordPress, which can make them inefficient compared with caching systems that operate on a lower level. Also, they’re usually huge plugins that cover all the nuances, which can make them hard to set up.
Hosting companies that offer WordPress-exclusive hosting often have their own caching systems that use even fewer resources and provide a more efficient experience. The main benefits include:
- You can forget about caching altogether.
- No additional plugins means no setup.
- They’re enforced across the whole hosting network, which results in a more efficient server.
- Speed benefits.
The biggest upside is that you don’t have to worry about it — everything is set up as efficiently as possible out of the box. Caching can get very technical, and whenever you set up a complex plugin, you’re bound to make some mistakes.
Compared with a perfectly coded theme and a perfectly set up caching plugin, you may not notice much of a difference. The fact that server-level caching doesn’t need to go through PHP to create files makes it faster, but this isn’t something you would notice normally. Most of the speed difference results from a well-oiled system out of the box.
If you have a website with enormous traffic and lots of moving parts, the caching system of a managed WordPress host may not offer enough options — but then it’s likely that neither will a plugin, and you’ll probably end up implementing your own solution.
I wouldn’t recommend choosing managed WordPress hosting just because it has its own server-level caching system, but it’s certainly a very nice addition! If you do choose to use managed WordPress hosting, be sure to choose one of the best, i.e. a company like WPEngine, Flywheel (read our review), or Kinsta.
Caching is a must-have feature for websites nowadays. It conserves resources while enhancing user experience.
As a website owner, you need to either pick a high-quality managed WordPress host or install a caching plugin and set it up — a small time investment for such an amazing feature.
If you have any additional tricks up your sleeve regarding caching, we’d love to hear them! Please share in the comments below!