Skip to content
Blog

WordPress object cache: what it is and when you need it

What a WordPress object cache is, how it differs from a page cache, when a site needs one, and how to turn on Redis and check that it works.

The WP Waterfall team · Guides

A WordPress object cache keeps the results of database queries and other expensive lookups in memory, so WordPress does not repeat the work on the next request. By default the cache lasts for one request only. A persistent object cache, usually Redis, keeps it between requests. You need one when many requests reach PHP, such as logged in users, a busy store, or a large admin, and you can skip it on a site that a page cache already serves.

Key takeaways

  • WordPress always has an object cache, but without a persistent backend it forgets everything at the end of each request.
  • A persistent object cache helps requests that reach PHP. A page cache helps requests that do not.
  • You can prove it works in one command with wp cache type, and see the effect in your database time.

What does the object cache do?

WordPress asks the database the same questions over and over: the site options, a post and its meta, a user, the terms of a post. The object cache is a key and value store that holds the answers. The next time code asks for the same thing, it reads memory instead of running a query.

The built in cache lives in PHP's memory and disappears when the request ends. A persistent cache is a separate service, most often Redis or Memcached, with a small file called a drop-in at wp-content/object-cache.php. WordPress loads that file instead of its default cache. Then request two can reuse what request one stored.

How is it different from a page cache?

A page cache stores the finished HTML of a page and serves it without running PHP at all. An object cache stores the pieces PHP needs while it builds a page. They work together:

Page cache Persistent object cache
Stores Whole pages Query results and other data
Helps Visitors who are not logged in Every request that runs PHP
Skipped for Logged in users, carts, checkouts Nothing, it is used inside PHP
Typical store Files, nginx, or a CDN Redis or Memcached

If you want the details of the first one, read What a page cache can and cannot do.

When do you need a persistent object cache?

You are likely to benefit when many requests cannot be served from the page cache:

  • Logged in traffic. Membership sites, course sites, and forums give every user a page that the page cache must skip.
  • WooCommerce. Carts, checkouts, and account pages are never cached as pages. See How to speed up a WooCommerce checkout.
  • A heavy admin. An editor team that works in wp-admin all day runs many uncached queries.
  • REST API and AJAX calls. These run PHP on every request.
  • Slow database time. If most of a slow request is spent in the database, fewer queries is the fix.

You can probably skip it when the site is mostly anonymous visitors reading pages that the page cache serves. The page cache already avoids the queries. A small brochure site gains little.

How do I check what my site uses?

Run these two commands with WP-CLI in the site folder:

wp cache type
wp plugin list --status=dropin

wp cache type prints Default when there is no persistent cache, and the name of the backend, such as Redis, when there is. The second command lists the drop-ins. An object-cache.php row means a persistent cache is installed.

How do I turn on Redis?

On any host where you run the server yourself, you need a Redis server, the free Redis Object Cache plugin, and two settings. These commands are the whole sequence, with a server on the same machine:

wp plugin install redis-cache --activate
wp config set WP_REDIS_HOST 127.0.0.1 --type=constant
wp redis enable
wp redis status

wp redis status should print Status: Connected and Drop-in: Valid. If the Redis server listens on another port, also set WP_REDIS_PORT the same way, with --raw so it is saved as a number.

How do I prove the cache persists?

Store a value in one command and read it in the next. With a persistent cache the second command finds it:

wp cache set greeting hello
wp cache get greeting

With Redis on, the answer is hello. With the default cache, the second command prints an error that the object was not found, because the first command's memory is gone. This is the simplest way to tell whether the drop-in works.

Common problems

  • Stale data after a database change. If you change the database outside WordPress, such as a direct SQL edit or an import, flush the cache with wp cache flush. WordPress itself clears the right keys when it saves content.
  • Two sites share one Redis. Give each site its own key prefix or database number, or one site will read another's cache. Good hosts do this for you.
  • A full cache. Redis can evict old keys when it reaches its memory limit. A cache that is too small works, but it misses more.
  • A broken connection. WordPress should fall back and keep working. Check with wp redis status.

How does it work on WP Waterfall?

WP Waterfall offers the object cache as an add-on called Redis Object Cache. It costs $100 a month for each site, with 2 GB of memory on live, and one charge covers live, staging, and every preview. A staging or preview environment gets up to half of its own memory, so the cache never starves the site. A test site gets it free. Open the site, choose Add-ons, and select Enable.

The server runs inside the site's own container, on a socket only that site can open. It is Valkey, an open source server that speaks the Redis protocol, so the Redis Object Cache plugin and any Redis client work unchanged. WP Waterfall loads the drop-in from outside your code, so it works on live even though live code is read only. The job does not finish until WordPress proves the cache works, and the Caching tab then shows memory used, keys, and hit rate. See Object Cache for the full steps.

To see whether you need it first, turn on Performance in Monitor for a few hours and open the Database tab. If database time dominates your slow pages, an object cache is worth trying.

Read next

Try what you just read about.

See it in your own dashboard.