Paulund
2012-08-17 #wordpress

How To Cache Queries In Wordpress

One of the best things about the Wordpress CMS is the power of it's built in APIs, there are multiple APIs which make it so easy for you to get and set things in the database. You should never need to edit the database tables, use SQL to insert data in the database or use SQL to get values from the database. One of the APIs which I find really useful in both plugin and theme development is the Transient API.

What Is Transient API

The transient API is a way of temporary storing data in the Wordpress database with an expiry time. This is mostly used for caching data as you can store data which will only be available for the next 10 minutes or hour. When the expiry time has past then you will not be able to get access to this data. When storing the data the transient API you will need 3 parameters a key/value pairing and an expiry time. Transient API should always be used to cache data which you expect to expiry. ## Set Transients

To set transients you need to use the set_transient() function, as noted above you will need to provide it with 3 parameters.


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

  • $transient_name - Unique ID to be given to the transient.
  • $value - The value to be stored
  • $expiration - The number of seconds to expire.

Get Transients

To get the transients you need to use the get_transient() function and provide it with the transient ID to get the correct value.


get_transient( $transient_name );

If the content of the transient does not exist or has already expired then the return of this function will be false. When you use transients the value should always be checked to see if it is false because progressing.


if(false === ($data = get_transient($transient_name) ) ){
     // Transient does not exist or has expired and $data must be set
} else {
     // $data has been set and is populated with the contents of the transient
}

Delete Transient

Transients are designed to expiry and delete when the expiry time has past, but you can force the transient to be deleted by using the function delete_transient(). This should be used when an action will mean the data in the transient is no longer valid.


delete_transient( $transient_name );

Putting It All Together

Now that we understand how you can use the different transient functions we can put these together to create a real world example of using Transient to get a list of Twitter updates and set this in a transient for 10 minutes.


$transName = 'list-tweets'; // Name of value in database.
$cacheTime = 10; // Time in minutes between updates.
if(false === ($twitterData = get_transient($transName) ) ){
   //Get new $twitterData
   $json = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$num");
   // Get tweets into an array.
   $twitterData = json_decode($json['body'], true);
   set_transient($transName, $twitterData, 60 * $cacheTime);
}

// Use $twitterData

As you can see from this example we first check to see if the transient exists by using the get_transient() function. If this has expired or doesn't exist then this function will return false. If the value of is false then we need to go and populate the $twitterData variable again. Now we have populated the variable with new data we can then add this to the set_transient() to reset the cache in the transient. After this if statement we can use the variable $twitterData and know that this will always be the cached data in this variable.