September 19, 2009

Memcached and WordPress MU

This week I, with help from many, got Word­PressMU setup to use mem­cached.

The instruc­tions for doing all this are on the Inter­net, but I thought it might help others (or at least my future self) to com­pile them all in one spot.

How WordPress caches

There are two layers of caching in Word­Press: * An object level cache, sim­i­lar to Django’s low-​level cache * Page level caching, pro­vided by plu­g­ins, sim­i­lar to Django’s per-​view cache

By the time we’re done, we’ll have both using mem­cache. The object cache defaults to the file system, and most folks use WP Super Cache which caches pages to the filesys­tem as well.

Object caching

global $memcached_servers;
$memcached_servers = array('default' => array('127.0.0.1:11211'));

You can add as many servers and change the port number to match your mem­cached setup. Example:

global $memcached_servers;
$memcached_servers = array('default' => array('bradfitzrocks:11211', 'bradfitzrocks2.mycompanydomain.com:90210'));

Stop here and you’ll be live with Word­Press object caching.

Page caching

<!--
	generated 12 seconds ago
	generated in 0.683 seconds
	served from batcache in 0.006 seconds
	expires in 288 seconds
-->
</head>
var $max_age = 300; // Expire batcache items aged this many seconds (zero to disable batcache)
 
var $remote = 0; // Zero disables sending buffers to remote datacenters (req/sec is never sent)
 
var $times = 2; // Only batcache a page after it is accessed this many times... (two or more)
 
var $seconds = 120; // ...in this many seconds (zero to ignore this and use batcache immediately)
 
var $group = 'batcache'; // Name of memcached group. You can simulate a cache flush by changing this.
 
var $unique = array(); // If you conditionally serve different content, put the variable values here.
 
var $headers = array(); // Add headers here. These will be sent with every response from the cache.
 
var $uncached_headers = array('transfer-encoding'); // These headers will never be cached. Apply strtolower.
 
var $debug = true; // Set false to hide the batcache info <!-- comment -->
 
var $cache_control = true; // Set false to disable Last-Modified and Cache-Control headers
 
var $cancel = false; // Change this to cancel the output buffer. Use batcache_cancel();

The Bat­cache ver­sion I linked to is a GitHub fork of Andy Skelton’s orig­i­nal ver­sion. My fork includes a patch to make sure the Content-​Type header is set cor­rectly on non-​HTML pages. I’m hoping to add some more cache inval­i­da­tion rules on top of the great work Andy already did.

Many thanks to the fol­low­ing folks:

Filed under: Programming,Technology

Next:
Previous:

Related

  • http://blogs.ajc.com/ Eric Bates

    One issue that we ran into recently had to do with hash values across mul­ti­ple mem­cached servers. We noticed that one of the cached objects wasn’t updat­ing mem­cached. We found that the hashes for that object were dif­fer­ent on each of our mem­cached servers. This prob­a­bly hap­pened when one of the servers became unre­spon­sive and tem­porar­ily dropped out of the pool. The default set­ting for PECL_MEMCACHED uses a “standard” hash­ing algo­rithm which works fine for single-​server imple­men­ta­tions. But when using mul­ti­ple mem­cached servers, you’re better off switch­ing to a con­sis­tent hash­ing algo­rithm which will retain the object’s hash key across mul­ti­ple servers.

  • http://tetalab.org Alex

    Hi,

    Thanks for your tuto­r­ial, t’s the most com­plete I’ve found yet with all the steps to install mem­cache on wpmu (might be miss­ing a bit of info on mem­cache install, but I’m sure people can figure it out).

    I cant suc­ceed in get­ting the stats in my source code, I sup­pose the system is not work­ing on my side, did you have a sim­i­lar prob­lem during your install? do you know a place where I can put a flag to debug the problem?

    Thanks a lot :)

    Alex

  • http://tetalab.org Alex

    Finally, I’ve found the root of the prob­lem, bat­cache doesn’t serve pages for con­nected users :)

  • http://heisel.org Chris Heisel

    Alex,

    Glad you fig­ured that out! Bat­cache, and I believe Super Cache both serve non-​cached pages to logged in users.

  • Scott

    Hi Chris, Thanks for post­ing this. I have an issue with this it seems to work for a bit (I see the cached info before the head) then it just stops the mem­cache server is still alive so that is not the issue. I also see SEG­FAULT errors as soon as I dis­able the seg­faults disappear?