Function: get_template_part_cached()

In my WordCamp Chicago presentation I had an bunk version of this function. Here’s a better version. Though we could probably still improve even this:

function get_template_part_cached($slug, $name, $key, $group = 'posts', $ttl = 3600) {
if( !$output = wp_cache_get($key, $group)) {
ob_start();
get_template_part($slug,$name);
$output = ob_get_contents();
ob_end_clean();
wp_cache_set( $key, $output, $group, $ttl );
}
echo $output;
}

Thesis Cache(r) Beta – 2012-07-31

Through my work at WPEngine I’ve found that using the Thesis Theme can occasionally lead to some performance issues on higher-traffic sites. Nothing spectacular, there are a ton of themes out there that have some issues scaling. Thesis is not special.

But what occurred to me about Thesis, is that it really should be one of the more performant themes out there.  The logic of the theme is organized hierarchically. Thesis passes all html output through thesis_html_framework() which means it is very easy to grab it and cache it, thus adding an additional layer of caching using the WordPress Object Cache.

[download link=”/downloads/thesis-cacher.zip”]

Why would you need this if you are already using W3TC or using a hosting company like WPEngine. Both give you various tools for page caching … why use another?

Well, first this plugin is not about replacing your existing caching, but instead making sure that you have adequate caching at the theme level. Even with system level caching there are going to be times where the cache is not served … either because a user is logged in, or because you are getting a lot of traffic and the cache is just missing, or because the cache was purposefully cleared.

ThesisCache(r) just gives you another layer to prevent consuming MySQL/PHP memory if you don’t have too. It gives you page-by-page,post-by-post control over whether to cache a post (or any post type). You can choose to NOT cache a page … or you can even choose to NOT cache the sidebars even though you want to cache the rest.

If you are not currently using ANY object caching no worries, ThesisCache(r) will set up a file-based object cache for you. If you are using an object cache, it will simply leverage what already use.

ThesisCache(r) isn’t in the Plugin Repo yet so here’s the download link. Please report any issues you have in the comments below. I’ll try to deal with them when I can.

[download link=”/downloads/thesis-cacher.zip”]

WordPress Portable Database Caching Class

So here’s a little library/class that I wrote to make caching a little easier from project to project. WordPress requires a plugin like W3 Total Cache to be in place for “persistent” caching to be available … that is, caches of data that survive longer than the current page view. But sometimes when you’re building a complex custom project there are some queries you know could be safely cached regardless of the global caching setup. With this class you can build it right into your theme or plugin.

WordPress Caching: Clear An Individual WordPress Post Cache

I’ve become a bit obsessed with caching lately. Speed is an addiction I suppose. So I found another new trick today. If your writing a plugin or theme and have some front in functionality adding and updating posts, sometimes you want to make sure a cached object is cleared before the page is refreshed. But you don’t want to dump the whole object cache, especially if there are thousands of posts. So to just dump one post you can simply do this:

wp_cache_delete($post_id,'posts');