Some of my recent work for Cheezburger has involved working with batcache, specifically leveraging it to use for A/B testing.
All of our sites like I Can Has Cheezburger?, FAILBlog and Memebase run on the amazing WordPress.com VIP service. I personally love it due to the fact that I don’t ever have to worry about servers, something I used to do way too frequently. Anyhow, being on WordPress.com does pose some interesting coding challenges. Many of the things you may come across with your own WordPress install can be solved by installing a plugin. On WordPress.com you need to roll your own solution, and check with them to make sure you’re not hosing all the millions of other blogs they host.
As stated above, I’ve been working on an A/B testing solution so we can try out new features, redesigns and services on the fly. We’ve done this in the past, but have always done it on the client side, and we wanted a more robust solutionâ€” one that didn’t involve inserting so much junk code into our theme. However, switching to server-side logic meant we needed to make WordPress.com’s caching solution, batcache, aware of what we’re doing. It took me quite a bit to wrap my head around it, but in the end the implementation is fairly simple. I won’t post the specific code here, but there’s a function where you can essentially tell batcache to serve a certain version of a page based on some simple logic (cookie, user agent, something that can be tested before the theme loads).
Armed with this knowledge, I set forth trying to get batcache running on my dev box, so I could ensure my code was performing correctly. First, I tried installing memcached, which turned out to be a nightmare. OS X comes with a default version of PHP installed, which led to me trying to compilie something for the wrong version of PHP. I eventually found that the code was being compiled for the wrong processor architecture, but I had hosed stuff too much to try to work around it. Fortunately for me, I was at WordCamp San Francisco while trying to do this, and attended Mark Jaquith’s awesome presentation, where he mentioned a plugin he had written for WordPress called APC Object Cache Backend. Here’s how I used MAMP and APC to get batcache running locally:
First: Open up MAMP preferences and click to the PHP tab. You’ll see a cache dropdown, which you should set to APC.
Second: Go download Mark Jaquith’s APC plugin and follow the installation instructions.
Third: Go download the Batcache plugin and follow the installation instructions (not the bit about memcached)
Last: Open up an incognito window in Chrome (you’re using Chrome, right?) and refresh your site a few times. You should now see the batcache header:
<!-- generated in 7.391 seconds 21506 bytes batcached for 300 seconds -->
Now you’re caching with batcache! I know it seems a little silly to run a cache locally, but I like knowing that the code I’m testing works with the actual end product.
If you want to read more about batcache, check out the author Andy Skelton’s blog!
Andy Skelton – Batcache for WordPress
I’ll try to blog more about what I’m working on in the future, and possibly some more on this A/B testing stuff!
3 thoughts on “Fun with Batcache and WordPress.com”
That’s just too easy. Nice work. Pleasure meeting you in SF.
You too! Thanks!
This is a nice article, but I’m afraid it doesn’t cover two essentials aspects that are not much discussed.
1. How to configure without changing a source controlled file (i.e. outside of the repo we clone on a deployment)
2. What is the relationship with `$vary` array of strings that gets eval’ed and the `$unique` array of strings.
To me those two aspects are unclear. Maybe the Batcache plugin *expects* us to “butcher” into the source controlled file and deal with our own fork ourselves.
Do you have recommendations about that?