Caching in WordPress
When we refer to “caching in WordPress” most users will think of some popular page caching plugins, or performance optimization plugins and tools. There is, however, a little more to it.
In this article we’ll take a step back and explore all the different layers where caching comes into play in a typical WordPress application. We’ll cover remote and runtime object caching, transient caching, query caching, fragment caching and of course page caching.
Caching 101
Given that you’re reading a tutorial about hosting WordPress in a Kubernetes cluster, we’re going to make an assumption that you have some familiarity with the term “caching”.
Whether it’s that thing that makes a slow website go fast, or perhaps that thing that you need to “flush” to fix things that may seem broken, caching is something that most WordPress users have dealt with at one point or another.
Most caching algorithms can be summarized in three simple instructions:
- Fetch data from storage and use if valid, otherwise:
- Perform some expensive lengthy computation
- Use the results, but also store them for future use
The “data”, “storage” and “valid” variables in these instructions are all vague and will depend on a specific implementation of a cache. We’ll explore some WordPress-related examples in this section, with object caching first.
Object Caching in WordPress
Every WordPress application, even one without any plugins or additional configuration, will have object caching. This is a set of functions provided by the WordPress core, that allow plugins, themes, and core itself, to cache arbitrary data under a specific key and group.
The storage for object caching in a default WordPress configuration is runtime memory in PHP itself. The implementation lives in the WP_Object_Cache class, where you will find the “storage” implementation:
// Holds the cached objects.
private $cache = array();
Yes, it’s that simple. It’s a PHP array (split by groups and keys) that can hold some arbitrary data for the duration of a single PHP request. You can dive into the get()
and set()
methods if you’d like to learn more about how this data is structured and accessed.
WordPress core makes very heavy use of this object cache, in particular when it fetches something from the database, like a post, a comment or a user, as well as slightly more complex structures, such as query results.
This article is for premium members only. Memberships start from $125/year and unlock access to all existing and future content on kubeadm.org, including all reference architectures.
Already a member? Login here
Getting Started
Running WordPress
Scaling WordPress
Scaling the Database
Caching
Ingress