Optimizing WordPress Object Caching with Memcached or Redis
In WordPress development, performance optimization often comes down to efficient caching strategies. While WordPress supports object caching out of the box, by default it is non-persistent — meaning the cached data is lost after each request. This post explores how to leverage Redis or Memcached to persist object cache and significantly boost your site’s performance.
Why Default WordPress Object Cache Falls Short
The native object cache in WordPress temporarily stores database query results during a single page load. Once the request completes, this data is discarded. As a result, every subsequent request must regenerate these results from scratch, leading to redundant database queries and increased server load.
To make caching effective, persistence is key. Persistent object caching stores the data beyond a single request so that it can be reused across page loads, reducing database overhead and accelerating page delivery.
Choosing Redis or Memcached for Persistent Object Cache
Redis and Memcached are high-performance, in-memory data stores widely used for caching. Both can store data in RAM, offering rapid access speeds far superior to querying a MySQL database directly.
Installing Redis and PHP Redis Extension
Redis is available in most Linux distributions and can be installed easily:
apt-get install redis
If you encounter an error like MISCONF Redis is configured to save RDB snapshots
, it indicates Redis is configured to persist data to disk. Since we’re using it solely for caching, you can disable this setting with:
redis-cli
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
Next, install the PHP Redis extension:
pecl install redis
Installing Memcached
Memcached setup is similar to Redis. After installing the Memcached service and PHP extension, make sure it’s running properly. We won’t go into full installation steps here, but documentation is readily available for most platforms.
Enabling Redis Object Cache in WordPress
To enable persistent object caching in WordPress, you need to override the default object cache system with a custom implementation. This is done by placing a file named object-cache.php
into your wp-content/
directory. This file acts as a “drop-in” plugin, replacing the internal caching mechanism.
Download the Redis object cache drop-in from the following repository:
👉 https://github.com/pressjitsu/pj-object-cache-red
Upload the file to your wp-content/
folder.
⚠️ If WordPress returns a 500 error after adding
object-cache.php
, verify that the Redis server is running and the PHP Redis extension is installed correctly.
Memcached Plugins for WordPress Object Cache
If you prefer Memcached over Redis, here are two popular plugins you can use:
Note: Some caching plugins like WP Super Cache may already include an object-cache.php
file. You can safely replace it with the Redis or Memcached version as needed.
Using WordPress Object Cache in Theme or Plugin Development
WordPress provides several functions to interact with the object cache:
wp_cache_add()
– Adds data only if the key does not existwp_cache_set()
– Adds or updates datawp_cache_get()
– Retrieves cached datawp_cache_delete()
– Removes a cached itemwp_cache_replace()
– Updates data only if it existswp_cache_flush()
– Clears all cached data
Sample Usage
$result = wp_cache_get('my_result');
if (false === $result) {
global $wpdb;
$result = $wpdb->get_results($query);
wp_cache_set('my_result', $result);
}
Object Cache vs. Page Cache
Page caching stores the fully rendered HTML output of a page, including template logic and database queries. This type of cache is very effective for static content and is often used on sites where users are not logged in.
Object caching, on the other hand, operates at a lower level, caching only database query results. It’s ideal for dynamic or user-specific content (e.g., logged-in dashboards) and can be used across both frontend and admin areas.
Both caching methods can be used simultaneously for optimal performance.
Conclusion
Persistent object caching with Redis or Memcached can drastically reduce database load and speed up WordPress websites. While caching is a powerful optimization tool, it should not be a substitute for writing efficient, performant code. Always strive for clean, optimized logic, and let caching enhance your already solid foundation.
By combining page cache for anonymous visitors and object cache for logged-in users, you can achieve an excellent balance of speed and flexibility.