How to Use Memcached as a Persistent Object Cache in WordPress

A common site health message WordPress displays for users is the recommendation to use a persistent object cache. There are generally two common solutions to this issue: Redis or Memcached.

This article will guide you in using Memcached to provide the caching solution that WordPress is recommending. We will walk you through its installation and configuration.

What is a Persistent Object Cache?

A persistent object cache is a server-side caching service that saves query results to a database. This type of caching improves database performance by reducing the number of queries the WordPress database processes.

This reduces CPU and memory resource usage on the server. You can tell your WordPress site is not using persistent object caching when you look at Site Health in your Tools menu:

Site Health - Persistent Object Cache recommendation

If your site is static and you do not have a lot of interaction with your viewers, then a caching solution like W3 Total Cache without Memcached will be sufficient.

How to Install Memcached

The installation of Memcached requires that you have root access on a VPS or dedicated server. Using Memcached will require a server-side install and then a plugin in WordPress. The server installation will need to be done through SSH. The plugin will be used to configure how WordPress will use Memcached caching.

Server Side Installation

The installation command for CentOS 7 is as follows:

# yum update
# yum install memcached

CentOS 7 will eventually be replaced with another Linux-based solution like Alma Linux. If you have opted to upgrade the server Linux system, then use this command:

apt -get install memcached

If you’re using a different flavor of Linux, then check your documentation for any installation differences.

Secure the Memcached Installation

The options setting must be changed to secure the Memcached installation so that only your local machine requests (your server) are listening to requests. You can use vi or nano to edit the file.

  1. After you have finished this installation, you will need to install tools to properly secure the Memcached installation.

    #yum install libmemcached
  2. Edit the configuration file for Memcached.

    #vi /etc/sysconfig/memcached.conf
  3. Change the OPTIONS line so that it is set as follows:

    OPTIONS = "-l -U 0"
  4. Restart the Memcached service to apply the configuration changes.

    #systemctl restart memcached
    #systemctl enable memcached

Plugin Installation – Using W3 Total Cache

You can use a WordPress plugin to use Memcached. We recommend using W3 Total Cache. See our guide for instructions on using W3 Total Cache.

  1. Log in to your WordPress Administrator Dashboard.
  2. Click on Plugins.
  3. Click on Add New.
  4. In the search field, type in “W3 Total Cache
  5. Click on the Install button.
  6. Click on the Activate button.

Configure W3 Total Cache to Use Memcached

Once you have installed and activated W3 Total Cache, you must configure it to use Memcached.

  1. Click on Performance in your WordPress Administrator Dashboard menu.
  2. If you have just installed W3 Total cache, click on Skip.

    W3 Intro screen - click skip
  3. Click on General Settings.
  4. Under Page Cache, check that it is set to Enhanced. It should be set by default.
  5. Find the Database Cache section and click Enable.
  6. After that, click on the drop-down box for Database Cache Method and select Memcached.

    Database cache - select memcached
  7. Click on Save Settings & Purge Cache.
  8. Next, go to Object Cache and select Memcached from the drop-down for Object Cache Method.
  9. Click on Save Settings & Purge Cache.

Once you have configured W3 Total Cache (or another plugin), your WordPress site is set to use Memcached. You can find more information by going to the Memcached wiki.

Using a persistent object cache improves your WordPress page load times by using previously saved queries. This is especially helpful when experiencing heavy traffic spikes on your site.

A good example of where using Memcached is an excellent solution for your caching needs is a large eCommerce site with many products and registered users. The caching aids the site performance by saving common queries for products and menus that help to reduce your resource consumption.

Check out our WordPress Educational channel for many other useful tips and tutorials!

Arnel Custodio Content Writer I

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel

2 thoughts on “How to Use Memcached as a Persistent Object Cache in WordPress

  1. there a typo in step two of memcache install above. it reads “#vi /etc/sysconfig/membached” <–with a "b"

    i am trying that step with the correct spelling but i am not getting any responses in terminal.

    1. Hello Oscar – thanks for pointing out that typo. I took a look at the command again and realized that it had more than 1 typo. It has been corrected – my apologies for the frustration with it. Here’s the correct command: #vi /etc/sysconfig/memcached.conf

Was this article helpful? Join the conversation!