Using Nginx with W3 Total Cache and Memcached

When working on a WordPress project recently where speed was a huge concern, I relied on everyone’s favorite reverse proxy, Nginx, to offload the work required by Apache to serve pages. This worked great, but an issue I encountered was invalidating cached items that were just updated (e.g. comments, post edits, etc.) in Nginx. For users to see the updated content, the cache TTL had to pass, which was unacceptable for our purposes.

I was already using W3 Total Cache in WordPress with Memcached storage, and although W3 Total Cache knew to invalidate certain cached items in Memcached, Nginx was not aware of this since it used its own local cache.

Nginx, thankfully, has built-in support for Memcached, and is therefore able to get key/value pairs that were previously set by the application (i.e. by default (without patching), it doesn’t store cached items to Memcached, but can read from Memcached). My hope was to let W3 Total Cache perform all the caching/invalidation, and let Nginx rely on that cache for content.

The problem, though, is that W3 Total Cache stores items in Memcached using a nonstandard keys, and stores the value as a PHP array. It also compresses values (via the Memcache PHP library) that are over a particular size. These incompatibilities made working with Nginx not possible–at least not without modification.

I took the time to modify W3 Total Cache so that it stores data in a format that Nginx can clearly understand. Once done, it was awesome. Comments appeared immediately, and pages were snappy.

I’ve upped the modified W3 Total Cache plugin here:

https://github.com/ameir/W3-Total-Cache

You will need to set a key to use (e.g. ‘ameir.net:’). Memcached keys will then look like ‘ameir.net:/path/to/article/’. In Nginx, you would set the key accordingly (set $memcached_key ameir.net:$request_uri;).

Enjoy!

Posted in Tech Corner
One comment on “Using Nginx with W3 Total Cache and Memcached
  1. JonG says:

    Perhaps you could post your nginx.conf file here, and add commit messages to your github repo to show which files have been modified? In its current state, this is interesting but not immediately useful.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">