Object Caching In Simple Terms
Every time someone opens a page on a WordPress site, WordPress collects what it needs to build that page, such as the site title, the menus, the posts, the widgets and the user who is logged in. Most of it lives in the database. Object caching is about not asking the database for the same thing twice.
What object caching is#
Object caching keeps a copy of data after it has been read from its original source, so the next time the same data is needed, the copy is used instead. The original source is usually the database, and reading a copy from memory is much faster than running a database query.
An example#
Take a page that shows the site title in three places, in the header, the main heading and the footer.
The first time the page needs the site title, WordPress reads it from the database and keeps a copy in its object cache. The heading and the footer then get the title from that copy. Three uses, one trip to the database.
WordPress does this for many kinds of data, such as options, posts, terms, users and comments. Plugins can use the same cache through wp_cache_get() and wp_cache_set(), like this.
$popular = wp_cache_get( 'popular_posts', 'my-plugin' );
if ( false === $popular ) {
// Not in the cache: run the slow query and keep the result for an hour.
$popular = my_plugin_get_popular_posts();
wp_cache_set( 'popular_posts', $popular, 'my-plugin', HOUR_IN_SECONDS );
}
It forgets after every page#
By default, the WordPress object cache lives in the memory of the PHP process that builds the page. Once the page has been sent, PHP finishes and the copy is thrown away. The next visitor, or the same visitor on the next page, starts again with an empty cache and runs the same database queries.
Persistent object cache#
A persistent object cache keeps the copy after the page is done, so every page after the first can use it. WordPress supports this through a drop-in file, wp-content/object-cache.php, which replaces the built-in object cache with one that keeps its data between pages.
When WordPress needs a piece of data, it now looks in three places, in this order.
- The object cache in PHP memory, for anything already read while building this page.
- The persistent cache, for anything read by an earlier page.
- The database, only when neither cache has it. The result is then saved in both caches for next time.
There are two common ways to store a persistent object cache.
- An in-memory server such as Redis or Memcached. It is fast, but it is a separate service that your host has to provide and keep running.
- Files on disk, which is what Docket Cache does. Each cached item is saved as a small PHP file, and PHP's OPcache keeps those files in memory, so most reads come straight from memory. It needs no extra service, which makes it a good fit for shared hosting.
When it helps most#
A page cache saves whole pages and serves them to visitors who are not logged in. It cannot help with pages that are different for each visitor, such as the WordPress admin, a WooCommerce basket or checkout, a membership area or a forum. Those pages are built fresh every time, and that is where a persistent object cache saves the most database work.
The two work well together. The page cache serves visitors who are not logged in, and the object cache speeds up everything else.
Keeping the copy fresh#
A cache is only useful if its copies are correct. When something changes, such as a post being edited or a setting being saved, WordPress deletes or replaces the cached copy, so the next page reads the new data. Cached items can also have an expiry time, after which they are read again from the source.
If a page ever shows old data, clear the whole cache with the Flush Object Cache button on the Docket Cache screen in the WordPress admin, or with wp cache flush.
Read more#
- Caching In WordPress, on page, object and opcode caching
- Data Storage, on how Docket Cache stores and reads its cache files
- OPcache Extension, on how OPcache keeps PHP files in memory