Skip to content

Object cache

WordPress's object cache keeps data it has read from the database, so the same data is not fetched twice. By default it lasts for one request. A persistent object cache keeps it between requests, which speeds up the pages a page cache cannot hold.

By
WP Ministry
Published

In short

  • Every WordPress site has an object cache. What most sites lack is a persistent one, which keeps the data from one request to the next.
  • A persistent object cache usually needs a cache server from your host, such as Redis or Memcached, and a plugin that connects WordPress to it.
  • It helps the dashboard, logged-in users and a store's checkout, which a page cache cannot serve.

While it builds one page, WordPress needs the same data many times: the site's settings, a post, a user. The object cache keeps each piece in memory under a name once it has been read, so asking again costs no trip to the database.

Two things go by this name. The one built into WordPress is, in the words of its reference, "non-persistent": the data stays in memory "only for the duration of the request" and is thrown away when the page is sent. A persistent object cache keeps the data between requests in a cache server such as Redis or Memcached, which WordPress's guide says the hosting provider has to offer. When someone says a site has an object cache, they mean a persistent one.

It is not a page cache, which stores finished pages and skips WordPress altogether. The object cache works inside WordPress while a page is being built. A transient is a single stored value with an expiry time, and it moves into the object cache when a persistent one is in place. OPcache stores compiled PHP code, not data. A CDN stores files outside your server.

Where you meet it

  • Site Health. Go to Tools, then Site Health. On a live site the Status tab shows one of three lines: "You should use a persistent object cache", "A persistent object cache is not required" or "A persistent object cache is being used". Under the first it adds "Your hosting provider can tell you if a persistent object cache can be enabled on your site."
  • A file in wp-content. A persistent cache works through a file named object-cache.php, placed there by the plugin that connects WordPress to the cache server. Under Site Health, then Info, the "Drop-ins" section lists it as "External object cache." WordPress's documentation says it is separate from a page cache's file and can work without the WP_CACHE line in wp-config.php.

How to look at yours

bash
wp cache type

Default means the built-in cache only. Any other answer, such as Redis, names a persistent one. WP-CLI's reference notes that this is a guess made from the file's code.

What goes wrong

Common questions

Does every WordPress site need a persistent object cache?

No. Site Health recommends one only when the site holds enough data to gain from it, and says "A persistent object cache is not required" otherwise. WordPress's hosting handbook says it is particularly helpful where pages are hard to cache, such as for logged-in visitors and stores.

Is Redis the object cache?

Redis and Memcached are cache servers: programs that hold data in memory. The object cache is WordPress's own. A plugin's object-cache.php file makes WordPress keep its object cache in that server.

Is it safe to flush the object cache?

Yes. It holds copies of data that WordPress can fetch again. The site does more work until the cache refills, and WP-CLI warns of that cost on a live site. On a multisite network a flush typically empties the cache of every site.

Not sure what is wrong?

Tell us what you see. We reply with the cause and a fixed quote, and the diagnosis is free.