Wordpress APC Object Cache Issues
November 8th, 2007I decided to upgrade my blog to Wordpress 2.3.1, add the WP-SuperCache plugin, and also the APC Object Cache for Wordpress [More Info]. Aside from a really old plugin that caused an error, the Wordpress upgrade went very well. I then moved on to SuperCache, and while I had a few issues, it was nothing the readme file could not address. Now, the major issue, the APC Object Cache.
First of all, you are probably asking yourself, what is the APC Object Cache for Wordpress. The first thing you need to know is APC, which stands for Advanced PHP Cache. If you are familiar with PHP you know it is compiled at runtime. This also means that sometimes you’re on-the-fly compiling the same script over and over. Enter the opcode cache, this allows ‘byte’ code, which is ready to be excuted, be run instead. It’s a big cache, and what you should get out of my very poor explanation, is that it speeds up execution of PHP scripts (sometimes by a factor of 2-3x).
Now enter Wordpress. This is the software I use for my blog, one of the more popular blogging tools, and it has a lot of add-on’s, and methods you can customize for yourself. One of these customizes is called the Object Cache. Wordpress is full of objects, each thing you see on the sidebar is an “object”, and a lot of the plugins, and backend work are all objects. The Wordpress Object Cache for APC (also available for other opcode cache’s) allows you to store some of these “objects” in the APC cache. This means even when you’re generating a page that is very dynamic, most of the page will still be cached for you, which means faster page loads, and lower server overhead.
So what’s the problem? The issue I believe comes from the new sidebar, which uses widgets that are both built-in and also from plugins. These ‘widgets’ make full use of the built in Wordpress cache (if available, and in my case the APC version of it). The real issue is with apc_store, this function takes in a key and some data, and allows you to cache it to APC. However it does not accept multi-level array’s. The new Wordpress tries to use multi-level arrays, causing the code to blow up. The easy/simple fix, use serialize/unserialize when going into the APC cache.
Here’s how I changed it.
The new set function, Lines 131-137 on Original:
function set($id, $data, $group = 'default', $expire = 0) {
$key = $this->key($id, $group);
$result = apc_store($key, serialize($data), $expire);
if ( false !== $result ) {
$this->cache[$key] = $data;
}
return $result;
}
The new get function, Lines 93-111 on Original:
function get($id, $group = 'default') {
$key = $this->key($id, $group);
if (isset($this->cache[$key])) {
$value = $this->cache[$key];
} else {
$value = apc_fetch($key);
if ($value !== NULL) {
$value = unserialize($value);
}
}
/* echo “Cache key: $key
“;
echo ‘Cache value:
‘;
var_dump($value);
echo ‘
‘;*/
if ( NULL === $value ) {
$value = false;
}
$this->cache[$key] = $value;
return $value;
}
This may end up using a bit more memory, and may have other issues, but at least it works :).
Update: Adding link to new file.
apc-object-cache.php Version 0.2.1





November 8th, 2007 at 12:16 pm
I thought perhaps I had landed on a stranger’s blog. Very clean. Very minimalist. I can only imagine that you have a Japanese garden behind the page.
I do have one comment about the coding. You changed the code to
}
/* echo “Cache key: $key”;
I think that I would have actually gone with
}
/* echo ‘HELLO Hello hello ‘elo: Cash is key > broke;
But it’s your call. Mine just seems to make more sense. (To me at least.)
November 9th, 2007 at 1:08 pm
Uh. Yeah. It’s all as clear as mud!
November 9th, 2007 at 6:24 pm
I never feel nerdy after visiting ericbyers.com
November 14th, 2007 at 6:05 am
Seems like there’s a small problem with apc-object-cache.php Version 0.2.1:
Parse error: syntax error, unexpected T_RETURN, expecting T_FUNCTION in /Local/Library/WebServer/Webtest/wp-content/object-cache.php on line 88
Looks like ‘return $result;’ is living outside ‘function delete()’
November 14th, 2007 at 12:04 pm
Jeff: whoops, I put a working one on my server, and then must have messed with it later. I just updated it to fix it. Thanks!