Home/ Journal Bens Projects Binary Moon Archives About Ben Gillbanks Subscribe to Binary Moon Updates

Subscribe to Binary Moon Website Updates, it's Free and Easy to Stay in Touch

Search Binary Moon

Search Binary Moon

WordPress caching, Part 2

As I mentioned in WordPress caching part 1, WordPress has built-in caching that can be hooked into by plugins such as W3 Total Cache and Batcache (developed by Andy Skelton who is employed by Automattic).

In this article I am going to explain how I make use of WordPress internal caching - also know as the persistant cache - to speed up my site (and in turn Elemental).

What the persistant cache does is store the data you tell it to in a variable, so that the next time you use the data on that page you don't have to touch the database. If you have software like Memcached and Batcache installed then the data will also be saved to memory - which means future page loads will skip the database entirely. There's some documentation on the WordPress caching on the codex.

This might sound like a really obvious thing to do but surprisingly few developers make use of it.

As an example in Elemental I created a simple widget that grabbed a list of recent posts by a specific author. Before I learnt about the post caching I was doing 1 mysql query to grab a list of posts and then running the standard WordPress 'loop', which was then doing an extra query per post to get the relevant post information.

The problem was it didn't matter what information I requested in my original database query, I would end up doing 6 queries (1 to get the list of posts, and then 1 for each of the 5 posts). Once I plugged in the caching the number of queries instantly dropped back to 1.

To use the caching you have to use the function wp_cache_add. Keep in mind that you should query all the post data in the table, even if you only need the title, this is because the cache may be used elsewhere on the page, and missing data will mean more data will need to be grabbed, which negates any savings you made.

So - what I started with was something like:

	$sql = 'SELECT ID, post_title FROM ' . $wpdb->posts . '
		WHERE post_author = ' . $primaryPostData['author'] . '
		ORDER BY post_date DESC LIMIT 0, ' . $postAmount;

	$posts = $wpdb->get_results($sql);
	if ($posts) {
?>
	<ul class="authorPosts">
<?php
		foreach ($posts as $p) {
?>
		<li><a href="<?php echo get_permalink($p->ID); ?>"><?php echo $p->post_title; ?></a></li>
<?php
		}
		wp_reset_query();
?>
	</ul>
<?php
	}
?>

... and after the caching I had the code:

	$sql = 'SELECT * FROM ' . $wpdb->posts . '
		WHERE post_author = ' . $primaryPostData['author'] . '
		ORDER BY post_date DESC LIMIT 0, ' . $postAmount;

	$posts = $wpdb->get_results($sql);
	if ($posts) {
?>
	<ul class="authorPosts">
<?php
		foreach ($posts as $p) {
			wp_cache_add($p->ID, $p, 'posts');
?>
		<li><a href="<?php echo get_permalink($p->ID); ?>"><?php echo $p->post_title; ?></a></li>
<?php
		}
		wp_reset_query();
?>
	</ul>
<?php
	}
?>

Note: This example is hugely simplified and won't work as a copy and paste job. It's just outlined to show the differences between the 2 methods.

Traditionally the first example would be considered quicker. You are requesting just the data you need (only 2 fields) and then displaying it. However since the post data is not in the query cache the usage of the 'get_permalink' function means that additional database queries have to be made to get the permalink shortcode.

In addition, any time you use WordPress built in functions to get the data for this post again, they will already be in the query cache.

Usage

Using the function is simple. In my example above you have to pass an array of the post data, the post id, and the cache type - to be used as a key for retrieving the data later.

wp_cache_add($key, $data, $flag = '', $expire = 0);
param: int|string $key The cache ID to use for retrieval later
param: mixed $data The data to add to the cache store
param: string $flag The group to add the cache to
param: int $expire When the cache data should be expired

You can read up more on the wp_cache functions and their usage on the official codex page.

Note: if you are using the query_posts command, or pretty much any other built in WordPress function, then the caching will be taken care of for you. It will also, where possible, make use of any data you save to the cache as well

4 Responses to “WordPress caching, Part 2” Leave a reply ›

  • That's pretty awesome! Apparently I've been living underground to miss this!

    If a caching plugin that caches the DB queries is running, that'd effectively have the same result though, wouldn't it? Seems a bit of a cop out, but seems to be the easier way(!).

    • Profile

      Hey Alex, good question! :)

      The two things are actually quite different. DB caching plugins store the results of DB queries, so if you execute the same query twice, it will be loaded from the cache rather than executed again. What this persistant cache does is store data about the results of the query. For example if you do a query to get a posts contents, and then do a query to get recent posts (which includes this post) and then do a query to get popular posts (which includes this post) then the data you need to display about that single post will already be stored in memory. Obviously it's not quite that straight forward but that's the easiest way I can think of to explain it :)

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.


Follow Me


Random Link-outs

The Binary Network links to all my websites
bengillbanks.co.uk - Ben Gillbanks

Ben Gillbanks

All my websites under 1 roof, the easiest way to find out what I do

Pro Theme Design - premium WordPress themes

Pro Theme Design

Premium WordPress themes by web design pros (erm... that includes me)

Binary Joy - gaming news and reviews

Binary Joy

Gaming news and reviews

Binary Sun - play free online games

Binary Sun

Play and download free and paid games (many made by me)

Gaming Angel - download and play games online

Gaming Angel

Stacks of shareware games, free to try and cheap to buy