← All posts

Object Cache Precaching

By · · 4 min read

Docket Cache has an option called Object Cache Precaching. It is off by default, and many people are not sure what it does. In short, Docket Cache remembers which cached values each page needed, and loads them first the next time someone opens that page. This post explains how it works, and when it is worth turning on.

The idea#

Every page in WordPress asks the object cache for many values, such as settings, posts, menus and user details. Without precaching, Docket Cache reads each value at the moment WordPress or a plugin asks for it, in the middle of building the page.

With precaching on, Docket Cache learns which values a page asks for. On the next visit to the same page, it loads those values at the very start of the request, before WordPress and your plugins begin. When the page then asks for them, they are already in memory.

On the first visit to a page, Docket Cache reads each value when the page asks for it, and saves the list of values the page used. On the next visit to the same page, Docket Cache loads the values on that list at the start of the request, so they are already in memory when the page asks.
The first visit records the list, the next visit loads it first

How it works#

  1. The first visit records. While the page loads, Docket Cache notes each value it reads from a cache file.
  2. The list is saved. At the end of the request, the list is saved in a small cache file of its own, linked to the page address. It is kept for one day, and only written again when the list changes.
  3. The next visit loads it first. When someone opens the same address again, Docket Cache reads the list and loads those values straight away.

The page address does not include the query string. So /shop/?orderby=price and /shop/?orderby=date share one list.

Where it helps most#

A page cache plugin, or a CDN, already serves most pages to visitors who are not logged in. Those pages never reach the object cache, so precaching does nothing for them.

Precaching helps pages that a page cache cannot serve.

The limits#

Precaching is kept small on purpose, so it does not make a page slower than it was.

Constant Default What it limits
DOCKET_CACHE_PRECACHE_MAXKEY 20 Values loaded first on each page, and values recorded in each group
DOCKET_CACHE_PRECACHE_MAXGROUP 20 Cache groups recorded on each page
DOCKET_CACHE_PRECACHE_MAXFILE 100 Pages that can have a list. A value below 200 falls back to 100.

Docket Cache also stops loading the list if the request is close to the PHP time limit.

Some requests never use precaching. These are form posts, WP-CLI, the REST API, admin-ajax.php, wp-cron.php, xmlrpc.php, robots.txt and favicon.ico. A few values are never recorded, such as the list of active plugins and the cron schedule, because they change too often to be worth it.

Turning it on#

Turn on Object Cache Precaching in Docket Cache > Configuration, or add this line to wp-config.php.

define('DOCKET_CACHE_PRECACHE', true);

Then use your site as usual. The first visit to each page builds its list, and later visits use it. To start again, click Flush Object Precache on the Configuration screen, or run this command.

wp cache flush:precache

The Constants page explains each precache option.

Other object caches do this too#

Docket Cache has had precaching since 2020. Some Redis object cache plugins have a similar option, often called prefetching, and it is usually off by default there too.

With Redis, every read is a request to another server. Prefetching there saves time by asking for many values in one request, instead of one request for each value.

Docket Cache has no server in between. Its cache files are read from OPcache memory, so each read is already fast. Precaching helps by having the values ready before the page asks for them.

Should you turn it on?#

Precaching does not make the cache read less data. It reads the same values, but earlier and all together. How much faster a page feels depends on your site.

So we suggest a simple test. If you or your users spend a lot of time in the admin, or your site has many logged-in users, turn it on for a few days. Compare how fast your busiest admin screens and account pages load. If you see no difference, turn it off again. Nothing else changes.