The WordPress Transients API is a simple and intuitive interface for temporarily storing cached data. If you are familiar with the Options API then you will feel right at home. This is great, right? Another API to learn that is similar to another API you may or may not know, why should you care? Well, in this blog post trilogy we hope to show why you should definitely care!
In Part I we will cover:
- What is the Transient API and why you should definitely care?
- The Transients API Itself
- A useful pattern using the Transients API
What is the Transients API?
The Transients API allows you to easily create, access, update, and destroy cached data in the database similar to the way the Option API works, with a couple of subtle yet powerful differences:
- Transients are not always stored in the database. Wait, what? Yes, Transients are not always stored in the database. Transients will seamlessly be stored in memory if a caching plugin is present, which can lead to substantial speed improvements.
- Transients can expire and most likely will expire, at least at some point.
Why You Should Definitely Care?
Think about it! Complex queries execute once and get cached, only executing again after the Transient expires. Widget output, Shortcode output, JSON data, CSV data, and more can all be cached in a Transient. In Parts II and III of this trilogy we explore some concrete examples showing the power of Transients, but for now just know, if you don’t already, you should definitely care about the Transients API.
The Transients API
set_transient( $transient, $value, $expiration );
- $transient – the key for the transient. Should be no more than 45 characters. Similar to an Options key.
- $value – the data you wish to store.
- $expiration – the lifespan of this transient in seconds. I recommend looking the WordPress Time Constants that were added in 3.5.
get_transient( $transient );
- $transient – the key for the transient. The same key used to set the transient.
delete_transient( $transient );
- $transient – the key for the transient. The same key used to set the transient.
In addition to the above the Transients API also contains functions for WordPress multisite. They behave the same way, the only difference is they should be used in conjunction with the WordPress multisite feature. For more information on the Transients API check out the Codex Page.
A useful pattern using the Transients API
Before we end Part I of this blog post trilogy lets see how we can put this all together. There are many ways the Transient API can be used to speed up your theme or plugin. Below is a general pattern I have found when using the Transients API in my own work.
if( data_is_cached() ){ // get_transient() !== false
load_data_from_cache(); // get_transient()
}
else {
load_data_from_expensive_src(); // anything "expensive"; WP_Query, API Call, etc...
cache_data(); // set_transient();
}
I know what your thinking, one size does not fit all. Well, you are right. However, this pattern should cover a fast majority of scenarios dealing with loading “expensive” data. Now you may be wondering why delete_transient() is not included in this useful pattern, I mean at this point we really only used 2/3 of the API. Do we have to delete a transient? They expire on their own, most likely, after all. Should you worry? This is a bit of a loaded question. One could argue why delete a transient when you could simply update the one you already have. Good point! Many times it may never be necessary to explicitly delete a transient, in which all you need is the pattern above.
In Part II we will dive into a concrete example using the Transients API. For now you will just have to wait at the edge of your seats.