Using memcached with WordPress object cache

I like the web pages to be snappy, including my blog. I found my blog to be bit slow after I moved to WordPress even after performance tuning. After looking around for a solution I came across memcache back-end for WordPress object cache which can be downloaded from here. You have to drop the file object-cache.php into wp-content/. You will have to specify the memcached servers by adding the following lines to wp-config.php.

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

My blog loaded faster than before, it was all good. Afterwards I wanted to install the memcache back-end for WP object cache on my mother’s blog using the same memcached server. I just dropped the wp-config.php file into my mother’s installation of WordPress. That’s when things became awry. My mother’s blog started to redirect to my blog :( . After looking at wp-config.php closely it was clear to me it was designed for WordPress MU. However I didn’t want to give up on using memcache with WordPress. Looking even closer I noticed that I can specify the global variable $blog_id and it would be perpended to the memcache object key. I added the following line to wp-config.php in my mother’s WordPress installation in addition to the two lines above and applied this patch (patched object-cache.php).

global $blog_id;
$blog_id = 'priyani_mohanjith_net';

That’s it, both my blog and my mother’s blog became fast and it was working without any issues. Hope this helps someone who wants to use the same memcached server with multiple installtions of vanilla WordPress. You cannot use this technique or memcache unless your blog is self hosted on a server where you have full control.

If you enjoyed this post, make sure you subscribe to my RSS feed!

30 thoughts on “Using memcached with WordPress object cache

  1. I have problems with multiple blogs in the same server. I user blog_id variable but wp-admin don’t run correct.

    I put memcache for carrero.es, herencia.net and decoracion2.com, and /wp-admin/ jump all time to the first access to carrero.es/wp-admin.

    I put herencia.net/wp-admin and jump tu carrero.es/wp-admin … why ??

    I need help :O now i desactivate memcache :(

  2. this seems to work for multiple blogs in wordpress mu or multiple vanilla wordpress…

    However this does not work with multiple installations of wordpress mu. Any way to get it to work instead of starting up another memcache server?

  3. You are not making much sense, do you want to use the same memcache server for two WordPress installations. Please read the post again, it is about using the same memcache server for two WordPress installations, whether it’s on two domains or not is insignificant.

  4. LUis, if you applied the patch properly and made the other configuration changes it will work. I’m using the same memcached server for mohanjith.net/blog and priyani.mohanjith.net/blog and have no such issues. I did have similar issues without the patch.

    Can you post your object-cache.php somewhere I can have a look?

  5. Can You post modified object-cache.php?

    I’m using windows system and don’t have shell access to my webhost.

  6. I get this error after following your instructions:

    [Wed May 19 01:23:53 2010] [warn] mod_fcgid: stderr: PHP Parse error: syntax error, unexpected ‘}’ in /var/www/clients/client1/web9/web/wp-content/object-cache.php on line 74

    any idea what could be wrong?

  7. btw, I didn’t apply your patch as I think I don’t need it… I am trying to implement memcache just for one wpmu installation

  8. Please follow the instructions given in the blog post, I do not maintain the plugin you mention. You might want to contact the plugin developer for help. As far as I know the plugin triggers a fatal error. If I get time I’ll create what’s discussed in this post to a plugin for everyones benefit :)

  9. Excellent Article! We’re now using memcache with WordPress sites and for our in-house Kaltura-CE install off one server. Thank you!

  10. I setup memcache with another wordpress site running on an OS X server. Memcache is installed and running.. however the site now loads about 10s slower. If I change object-cache.php to object-cache.php_ the site instantly goes back to loading in just under 2s. Any idea why using the object-cache would slow the site down this much? Thanks!

  11. It could be either because the key-value pairs are not being cached (or they are being removed from the cache due to memcached running out of memory) or network connection to the memcached server has very high latency. You should try to see why. Under normal circumstances using memcache (object-cache.php) will speed up page load time.

Leave a Comment