Use Memcached or Redis to Improve WordPress Object Cache Performance

WordPress object caching is the mechanism WordPress uses to cache the results of expensive database queries. By default, the object cache is non-persistent. It only stays in memory during the current request, and once the request ends, the cache is gone. On the next request, it has to be generated again.

The efficiency issue of the default WordPress object cache

Anyone familiar with caching will notice that this mechanism is not very efficient. To improve the efficiency of object caching, we need to persist the cache. In other words, we need to keep the cached results instead of letting them expire at the end of each page request. That way, the next page request can use the saved cache result directly instead of querying the database again.

Use Redis or Memcached to persist object cache data

Redis and Memcached are both well-known in-memory databases. Because they can store data directly in memory, they can greatly improve data access speed, and they are often used as cache databases in front of MySQL.

Install the Redis service and the PHP Redis extension

Ubuntu, Debian, and CentOS all provide Redis packages, so in most cases we can simply install the service directly with the corresponding package manager command. Of course, if you want more control, you can also download and compile the Redis server from source.

apt-get install redis

After installing the Redis server, if it fails to start and reports the error MISCONF Redis is configured to save RDB snapshots, we can use redis-cli to disable the disk snapshot requirement. Since we are only using Redis as a cache service here, we do not need it to write data to disk.

127.0.0.1:6379> config set stop-writes-on-bgsave-error no

After the Redis service is installed, we still need to install the PHP Redis extension so WordPress can actually cache data into Redis.

pecl install redis

The installation process for Memcached is similar to Redis, so I will not repeat it here. After installing Redis or Memcached and confirming the service is working, we still need to enable Redis or Memcached object caching in WordPress so those services can persist the object cache for us.

Enable Redis object caching in WordPress

The usual way to enable persistent object caching in WordPress is to replace WordPress’s default cache functions with a custom cache implementation. The way to do that is to add an object-cache.php file to the wp-content/ directory. In essence, object-cache.php is a WordPress “drop-in” plugin used to replace built-in behavior.

To store object cache data in Redis, download object-cache.php from the following project and upload it to the wp-content/ directory of your WordPress site.

https://github.com/pressjitsu/pj-object-cache-red

Memcached object cache plugins

If you are using Memcached to persist the WordPress object cache, either of the following drop-ins will work.

Some plugins automatically add an object-cache.php file to the wp-content/ directory. For example, the popular WP Super Cache plugin can do that. In that case, simply replace the existing file.

If the site starts returning a 500 error after you add object-cache.php, that usually means the Redis service or extension has not been installed correctly. Check that the service and extension are available first, then add object-cache.php again.

Use object cache while developing WordPress themes or plugins

WordPress provides a set of object cache functions that make it convenient to work with the cache in your own code.

  • wp_cache_add(): add data to the cache. If the data already exists, it returns false.
  • wp_cache_set(): add data to the cache. If the data already exists, it overwrites it.
  • wp_cache_get(): get data from the cache. If the data does not exist, it returns false.
  • wp_cache_delete(): delete data from the cache.
  • wp_cache_replace(): replace existing cached data. It is similar to wp_cache_set(), but if the data does not exist, it does not add it automatically.
  • wp_cache_flush(): clear all cached data.

Example of using the WordPress object cache

$result = wp_cache_get( 'my_result' );
if ( false === $result ) {
    $result = $wpdb->get_results( $query );
    wp_cache_set( 'my_result', $result );
}

The difference between object cache and page cache

This site has previously introduced the method of using Cachify to cache WordPress pages in order to improve page loading speed. This method of directly caching the pages generated by WordPress is called “page caching”. What it caches is the generated HTML pages themselves, so it caches not only database queries but also the results of PHP logic inside the template.

Object caching, by contrast, only caches MySQL query results. It does not necessarily cache other kinds of PHP computation. Compared with page caching, it works at a lower level, the cache granularity is finer, and it is easier to control. On the other hand, WordPress object cache can cache not only front-end database queries, but also query results inside the dashboard, so it can improve WordPress admin performance to some extent.

Both methods can greatly improve WordPress page loading speed. Page cache is suitable for sites that do not rely on logged-in users. Object cache is more suitable for sites where users need to log in and interact with the site, so you can avoid page cache accidentally storing content that should only be visible after login.

Summary

Whether you use page cache or object cache, the purpose is the same: improve data retrieval efficiency and speed up the site. Object cache and page cache do not conflict. They can be used together to complement each other. Even so, faster caching does not mean we can ignore performance during development. Keeping data-access efficiency in mind from the start is still the key to building high-performance WordPress applications and improving user experience.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *