Skip to main content

Memcached Object Cache for WordPress

In this lesson you will learn how to install and configure a Memcached service, the memcached PHP extension, and a WordPress drop-in for persistent object caching using Memcached.

Note: This lesson covers the Memcached object cache backend for WordPress. If you're looking to use Redis, refer to the Redis Object Cache lesson instead.

Memcached is a very fast in-memory key-value store that predates Redis, and remains one of the simplest and fastest caching systems available. It's widely used by large web platforms and WordPress hosting providers, such as WordPress.com and Pressable.

Unlike Redis, Memcached is designed exclusively for caching and offers no data persistence. This makes it extremely lightweight and a great fit for the WordPress object cache.

Installing Memcached

Memcached is available in the default Ubuntu repositories and can be installed using the apt package manager:

sudo apt install memcached

The default configuration file lives in /etc/memcached.conf. Let's move that to our /config repository for version control, and symlink it back to its original place:

sudo mv /etc/memcached.conf /config/misc/memcached.conf
sudo ln -sfn /config/misc/memcached.conf /etc/memcached.conf

The Ubuntu-packaged default configuration file binds Memcached to local interfaces only, on the default port 11211. It binds to IPv6 by default too, so if that's not available on your system, make sure to comment out the corresponding -l line, otherwise Memcached will fail to start.

The default memory allocation is 64 MB, which is quite low for a typical modern WordPress site, and especially low if you're planning to serve multiple sites. Here's an updated config adjusted for 1 GB, and no IPv6:

-d
logfile /var/log/memcached.log
-m 1024
-p 11211
-u memcache
-l 127.0.0.1
-P /var/run/memcached/memcached.pid

Use systemctl to restart the Memcached service:

sudo systemctl restart memcached.service

You can use telnet to ensure Memcached is accepting connections, as well as get useful information about the stored items, memory usage, cache hit rate, and more:

telnet localhost 11211
stats
quit

Use the quit command to close the connection and exit the telnet shell.

Note that the logfile option is unused when running under systemd, since the logs are written to journald instead. You can use journalctl to read these logs under the memcached unit:

sudo journalctl -u memcached

Now that we have a Memcached service up and running, let's make sure WordPress can communicate with it.

Memcached Object Cache for WordPress

There are a couple of Memcached extensions available for PHP. The most supported and up-to-date version at the time of writing is memcached, available via PECL and also packaged for Ubuntu and other apt users:

sudo apt install php-memcached

php -m | grep memcached
php-fpm8.3 -m | grep memcached

Note that there is another popular extension on PECL called memcache (without the d) which is slightly older, less actively maintained, but still works.

Next, let's install the WP Memcached plugin for WordPress. This is a fork of Automattic's previous Memcached Object Cache plugin, with added support for the memcached PHP extension:

cd /sites/uncached.org/public_html
wp plugin install https://github.com/Automattic/wp-cache-memcached/archive/refs/heads/develop.zip

This plugin does not need activation and will not automatically link the necessary object-cache.php drop-in file, so this must be done manually. However, the default PHP extension used is still memcache, and we'll need an extra flag in our wp-config.php to use the memcached extension instead:

define( 'WP_CACHE_KEY_SALT', 'some unique salt' );
define( 'AUTOMATTIC_MEMCACHED_USE_MEMCACHED_EXTENSION', true );

Let's now "activate" this plugin by creating the required symlink:

cd /sites/uncached.org/public_html/wp-content
ln -s plugins/wp-cache-memcached/object-cache.php object-cache.php

WordPress core loads the object-cache.php file automatically when it exists. Renaming or deleting this symlink will effectively "deactivate" the plugin.

Testing persistence

The WP Memcached plugin does not have any settings panel in the wp-admin dashboard, nor does it have any CLI commands. The integration is completely transparent, and the only simple way to test it is by using WordPress core's cache functions.

This article is for premium members only. One-time payment of $96 unlocks lifetime access to all existing and future content on wpshell.com, and many other perks.