WordPress

Using Memcache proxy for caching pages

This post is deprecated.

This is the forth post of “Running WordPress on Managed VMs” series. On Managed VMs, you can use Memcached service(the link is for Python runtime, but the basic is the same) out of the box. In this post, I’ll show you how to use WordPress plugins to use the Memcached service to get better performance.

Which WordPress plugin to use?

There are many cache related plugins, but which is the best plugin for using it with Managed VMs? WP Super cache is very famous one, but it generates its cache on the local file system, so that you should take care of clearing the cache on multiple servers, which is theoretically possible, but somewhat hard to do it correctly. In the future, I will explore this option, but for now, I’d go with an easier way of using the Memcached service provided by App Engine infrastructure because the cache in the Memcached service can be used by multiple instances. In this post, we’ll use Batcache and Memcached plugins.

Install plugins

Download those plugins locally into the `wordpress/wp-content/plugins` directory. These 2 plugins are so-called drop-ins. You need to manually copy some files into the `wp-content` directory. Unfortunately these plugins don’t work locally with the PHP built-in web server, so I wrote a shell script wrapper for copying those files when necessary.

deploy-wrapper.sh:

#!/bin/sh

set -x

# Temporary copy the dropins

cp wordpress/wp-content/plugins/batcache/advanced-cache.php \
    wordpress/wp-content/advanced-cache.php
cp wordpress/wp-content/plugins/memcached/object-cache.php \
    wordpress/wp-content/object-cache.php

$@

# Remove the file for local run
rm wordpress/wp-content/advanced-cache.php \
    wordpress/wp-content/object-cache.php

Then add the cache configuration in wordpress/wp-config.php:

// Cache settings
define('WP_CACHE', ON_MVM);

$memcached_servers = array(
    'default' => array(
        getenv('MEMCACHE_PORT_11211_TCP_ADDR')
        . ':' . getenv('MEMCACHE_PORT_11211_TCP_PORT')
    )
);

Deploy and check if it’s working

Now you can use the shell script for the deployment:

$ sh deploy_wrapper.sh gcloud app deploy \
  --promote \
  --stop-previous-version \
  app.yaml

Then you should see these 2 drop-ins are activated as follows:

WP-dropins

To make sure it’s really working, you can open an incognito window and visit the site because the cache plugin only serves from cache to anonymous users. Also you should access the site several times because the plugin only caches pages which are considered popular. You will see the following Batcache stats in the HTML source:

<!--
	generated 31 seconds ago
	generated in 0.165 seconds
	served from batcache in 0.009 seconds
	expires in 269 seconds
-->

Conclusion

Google provides the Memcached service for Managed VMs instances out of the box and it’s free. It is very easy to use without any preparation, you just need to read 2 environment variables. By enabling the cache, the site will be much faster than before. Note that the Memcached service is only appropriate for caching purpose. You shouldn’t use it for permanent storage, because the cache data may be evicted any time. With the free version, you will likely see more frequent cache eviction, but since it’s free, there is no reason to not to use it. There is also a paid version of the Memcached service. The paid version provides dedicated memory space only for your app, so you’ll see less frequent cache eviction.

In the next post, I’ll show you how to configure WordPress on Managed VMs from scratch.

Leave a Reply

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

By submitting this form, you accept the Mollom privacy policy.