What is an Object Cache in WordPress?
An object cache in WordPress stores query results and frequently accessed data in server memory. This reduces the need to call the database for the same information on every page load, improving performance and response times.
How Object Caching Works
Each time a user accesses a WordPress page, the server processes PHP code and queries the database to fetch content. With object caching enabled, the results of those queries are stored in memory.
On repeat requests during the same session, cached values are served directly from memory without repeating the database query.
WordPress first checks for the requested data in the object cache. If found, it uses the cached value. If not, the data is queried from the database and then added to the cache for later use.
WP_Object_Cache
WordPress includes a built-in caching component called WP_Object_Cache. This class loads during every page load and stores object data in PHP memory.
By default, the WordPress object cache is non-persistent. It only keeps data in memory for the current request. Once the page finishes loading, the cache is cleared.
Persistent Object Caching
Persistent object caching allows the cached data to survive between page loads. This provides better performance for recurring queries and commonly accessed content.
Tools like Redis and Memcached provide support for persistent object caching. They store cached objects in server memory and manage them using policies like Least Recently Used (LRU) to remove old data when the cache is full.
To use persistent caching, you must install and configure a compatible plugin, such as:
- Redis Object Cache
- W3 Total Cache (with object caching enabled)
Available Functions
WordPress includes functions that interact with the object cache:
- wp_cache_set( $key, $value, $group = ”, $expire = 0 ) – adds or updates a value in the cache.
- wp_cache_get( $key, $group = ” ) – retrieves a value from the cache.
- wp_cache_delete( $key, $group = ” ) – removes a value from the cache.
These functions allow developers to build efficient server-side logic by limiting the need to process duplicate queries or computations.
Example Use Cases
Object cache is helpful when running expensive queries on every page. For example:
- Custom queries that fetch post metadata
- User data pulled multiple times per load
- Plugin functions that perform repeated joins or counts
In high-traffic environments, such as WordPress VIP, each site can have its own Memcached cluster. This helps in serving data from memory and reduces the burden on the primary database.
Transients and Object Cache
Transients are a method for storing data for a limited time in WordPress. Under normal conditions, transients are stored in the options table. However, when a persistent object cache is active, transients are stored in the memory cache instead.
This means code or plugins relying on reading transients from the database may not function correctly if the data is no longer saved there. To manage transients in a persistent cache, developers can use tools like WP-CLI with the wp transient commands.
Cache Conflicts and Misconfiguration
Running multiple caching systems at the same time can cause problems. For example, object cache and page cache solutions may conflict if they attempt to cache overlapping parts of the site.
Misconfigurations can lead to:
- Stale data being served
- Cache not clearing after changes
- Slowdowns due to ineffective caching strategy
It’s recommended to use one object caching tool alongside one page or browser caching method and to test configurations carefully.
Monitoring Object Cache Performance
To monitor object cache usage and effectiveness, tools like Query Monitor can show detailed metrics:
- Cache hit rate
- Execution time of cache retrieval
- Number of objects stored
- Amount of memory used
These insights help developers detect bottlenecks and decide whether caching is working as expected.
Installation Process
To set up persistent object caching using Redis, for example:
- Install the Redis Object Cache plugin.
- Activate the plugin from the WordPress dashboard.
- Click the “Enable Object Cache” button in the plugin settings.
The plugin connects your site to the Redis server and replaces the default object cache behavior with persistent storage.
Summary of Key Points
- Object cache stores query results in memory during a page load.
- The default WP_Object_Cache is non-persistent.
- Persistent caching through Redis or Memcached allows data to remain cached across loads.
- Cache functions include wp_cache_get, wp_cache_set, and wp_cache_delete.
- Use a single object cache configuration to avoid conflicts.
- Monitor cache behavior using tools like Query Monitor.