User Agents

Web browsers and everyday web tech, explained

WordPress Object Cache: What It Is & How It Works

· 6 min read · Cookies and Cache

In this article
  1. How the Default Object Cache Works
  2. Persistent Object Caching (Redis, Memcached, APCu)
  3. Why Use Object Cache? Benefits and Trade-offs
  4. How to Enable and Configure Object Cache
  5. Common Problems and Misconceptions
  6. Object Cache vs. Other Caching Layers
  7. Cost and Hosting Considerations
  8. Frequently Asked Questions

WordPress object cache is a mechanism that stores database query results in memory so that repeated requests for the same data don’t hit the database again. When a page loads, WordPress runs many SQL queries to fetch posts, options, comments, and user data. The object cache holds those results in an array or a faster storage backend (like Redis or Memcached) for the duration of the request, or longer if a persistent cache is enabled. This reduces database load and speeds up page generation, especially on high-traffic sites.

How the Default Object Cache Works

By default, WordPress uses a non-persistent object cache that lives only within a single PHP request. Every time a page is loaded, the cache is built from scratch and then discarded when the request ends. This is built into the core via the WP_Object_Cache class, which stores data in a PHP array. Functions like wp_cache_get(), wp_cache_set(), and wp_cache_delete() interact with this cache.

Because the default cache is non-persistent, it doesn’t help across different page loads. Each visitor triggers a fresh set of database queries. However, it does prevent duplicate queries within the same request—for example, if two plugins call get_option('siteurl'), the second call returns the cached value instead of querying the database again.

Persistent Object Caching (Redis, Memcached, APCu)

To get real performance benefits, you need a persistent object cache that stores data in memory across requests. Popular backends include:

  • Redis – an in-memory data store that supports data structures like strings, hashes, and lists. It’s the most common choice for WordPress object caching.
  • Memcached – a distributed memory caching system, often used in shared hosting environments.
  • APCu – a PHP opcode cache that also provides a user cache, suitable for single-server setups.

These backends are accessed through a drop-in file called object-cache.php placed in the wp-content directory. This file replaces the default WP_Object_Cache class with one that connects to the external cache server. When a value is set, it’s written to memory; when fetched, it’s read from memory, bypassing the database entirely.

Why Use Object Cache? Benefits and Trade-offs

The main benefit is speed. Database queries are often the slowest part of a WordPress request. Caching query results in memory can cut page load times significantly, especially for sites with many queries or high traffic. It also reduces the load on your database server, which can help avoid bottlenecks.

Trade-offs include:

  • Stale data – cached values can become outdated if not invalidated properly. WordPress handles this by clearing cache entries when posts are updated, comments are added, or options change.
  • Memory usage – Redis or Memcached consume RAM. On a small server, you might need to allocate memory carefully.
  • Complexity – setting up a persistent cache requires server configuration and a drop-in file. Misconfiguration can lead to errors.

For most small sites, the default non-persistent cache is sufficient. Persistent caching becomes valuable when you have moderate to high traffic or a large database.

How to Enable and Configure Object Cache

Enabling a persistent object cache generally involves these steps:

  1. Install and start a caching server (e.g., Redis) on your hosting environment.
  2. Install a WordPress plugin that provides the drop-in file, such as Redis Object Cache or W3 Total Cache (which includes Redis/Memcached support).
  3. Activate the plugin and configure the connection settings (host, port, password).
  4. Verify that the cache is working by checking the plugin’s status page or using a debugging tool.

Alternatively, you can manually place a custom object-cache.php file in wp-content that uses a caching library like Predis for Redis or php-memcached for Memcached. However, using a well-maintained plugin is safer and easier for most users.

Common Problems and Misconceptions

A frequent misconception is that object cache is the same as page cache. Page cache stores the fully rendered HTML output, while object cache stores raw data (like query results). They serve different purposes and can be used together.

Another common issue is stale data. For example, if a plugin updates a post but doesn’t call wp_cache_delete() for that post’s cache key, the old data may be served. WordPress core tries to invalidate caches on relevant actions, but third-party plugins may not always follow best practices. If you notice outdated content, clear the object cache manually (e.g., via the plugin’s admin page or wp cache flush command).

Also, some hosts may not allow external connections to Redis or Memcached due to security policies. In that case, consider APCu or a file-based cache (though file-based is slower).

Object Cache vs. Other Caching Layers

WordPress has several caching layers, each with a distinct role:

  • Page cache – stores the final HTML output. Served by plugins like WP Super Cache or server-side solutions like Varnish.
  • Object cache – stores database query results and PHP objects.
  • Transients – a specific API that uses the object cache if available, otherwise falls back to the database options table. Used for temporary data like API responses.
  • Opcode cache – caches compiled PHP files (e.g., OPcache). Not directly related to object cache but affects overall performance.

Understanding the difference helps you choose the right caching strategy. For example, if your site has dynamic content that changes per user, page caching might not be suitable, but object caching still helps by reducing repeated queries.

Cost and Hosting Considerations

Persistent object caching often requires a server with sufficient RAM. On managed WordPress hosting, Redis or Memcached may already be available—check your host’s documentation. Some hosts charge extra for dedicated caching servers, while others include it in their plans. If you’re on a low-cost shared host, you might not have access to these services, and installing them manually could be impossible.

For DIY setups, running Redis on a VPS is free (open-source), but you need to manage the server yourself. The cost is mainly in time and server resources.

Frequently Asked Questions

Does WordPress object cache work without a plugin?

Yes, the default object cache is always active, but it only works within a single request. To get cross-request caching, you need a persistent backend and a drop-in file, which is typically provided by a plugin.

How do I clear the object cache in WordPress?

If you’re using a caching plugin, there’s usually a “Flush Cache” button in its settings. Alternatively, you can use WP-CLI command wp cache flush if you have command-line access. For the default non-persistent cache, it clears automatically at the end of each request.

Is object cache safe to use?

Yes, when configured correctly. The main risk is stale data, which WordPress mitigates through cache invalidation hooks. Ensure your caching plugin is up to date and test your site after enabling it.

Can object cache improve my site’s PageSpeed score?

It can indirectly help by reducing server response time, which is a factor in PageSpeed. However, the biggest impact on PageSpeed usually comes from page caching, image optimization, and minification.

What happens if the Redis server goes down?

If the object cache backend becomes unavailable, WordPress will fall back to the database for all queries, but it may also generate errors if the drop-in file doesn’t handle the failure gracefully. Most plugins will log an error and continue without caching, but your site may slow down temporarily.