Blog

Tech Tip: Caching WordPress with the Transients API

  |   General   |   No comment

During the development of a particularly complex WordPress integration our development team came up against severe performance issues when integrating a third party API.

 

Whenever your page load times are dependant on an external API’s load time you’re bound to come across this issue. This could be the Twitter API or a complex integration with a back office CRM system – both can have a major impact on your load times.

 

As your site grows and grows, the issue is likely to grow with it, this is where caching can help – do you need every single visitor to your site to see 100% live information from that API? Would it matter if the data was 5 minutes old, 15 minutes old … an hour? a day even? If live data isn’t critical then caching is your friend.

 

There are several techniques for caching in WordPress, mainly you’ll hear about W3 Total Cache and WP Super Cache. But these plugins don’t suit everyone, because they are built primarily to cache the whole page – which can get complicated if you have multiple sources of dynamic data on a page, specifically visitor specific data – as every user will see exactly the same page.

 

If this doesn’t matter to you, and you want every user to see exactly the same page as everyone else, use W3 Total Cache or WP Super Cache – they are easier to implement than what will be discussed here.

 

The second issue with those plugins is that they are devised to cache the front end site – but what if you need to cache data for use in a plugin, custom admin panel or field?

 

What we will discuss here is caching small amounts of data for use in a dynamically built page, a small bit of data that will be the same for every user (e.g. your latest tweet extracted from the Twitter API, weather data from the Met Office API, or even a list of products extracted from a back office stock control system)

 

WordPress calls these nuggets of information “Transients” (a word meaning “passing with time” or “of short duration”). Transients by default are stored in the WordPress options table, where they can be called upon from within your template or plugins.

 

Creating a transient is simple:

 

set_transient($name, $value, $expiration);

 

A few things to note, the transient name (which should be unique) should be no more that 45 characters in length, due to the way they are stored in the database and the expiration value should be specified in seconds.

 

Then to retrieve a transient you would use:

 

get_transient($name);

 

The ‘get_transient’ function will return ‘false’ if the transient either doesn’t exist or the transient has expired.

 

Put together you can use these functions along with your API call (or other time/resource draining logic) to cache the resulting data to speed up you page.

 

// This first line will both check if a transient exists and if so retrieve it into our $product_list variable

if (false === ($product_list = get_transient('warehouse_product_list'))) {
// Otherwise, we will generate the data to put into our transient for the next visitor to use

// Our long running, resource hogging API call…
$product_list = get_warehouse_product_list();

set_transient('warehouse_product_list', $product_list, 60 * 60 * 24);
}

// Use $product_list as normal from here...

 

It’s as simple as that – but of course you can make it as complicated as you like (it is WordPress after all!). You can find more information about the WordPress Transient API over at the WordPress Codex.

No Comments

Sorry, the comment form is closed at this time.