16

I've never used the Transients API before and was wondering if anyone has guidance on when to use it. The Codex article implies that as a theme developer I might want to set each new WP_Query() as a transient; I assume the same might be said for direct $wpdb queries and query_posts(). Is that overkill? And/Or are there other places I should default to using it?

I do often use caching plugins on my site (W3 Total Cache usually) and it sounds like using Transients might bump up the plugin's effectiveness but I don't want to go crazy wrapping everything in transients if that's not a best practice.

1
  • Thanks everyone, I wish I could mark more than one answer as the 'solution'. Great info, much appreciated!
    – Michelle
    Commented Mar 12, 2012 at 14:13

5 Answers 5

16

Transients are great when you're doing complex queries in your themes and plugins. I tend to use transients for things like menus and showing other things like Tweets from Twitter in a sidebar for example. I wouldn't use them for absolutely everything more-so just temporary pieces of data that can be cached.

Keep in mind that if you use something like Memcached with transients, then you will notice a massive performance gain. The rule with transients is to not use them for data that should not expire as they're really only for temporary data and keep in mind transients are not always stored in the database.

Some uses for transients:

  • Complex and custom database queries
  • Wordpress navigation menus
  • Sidebar widgets that display info like; tweets, a list of recent site visitors or a Flickr photo stream
  • Caching tag clouds

This article is a very informative one with quick benchmarks showing just how transients can speed up your site and even has a couple of examples. This other article also has a few great examples of using transients which might help you understand what to use them for as well.

1
  • 2
    Another use: caching external HTTP requests. Like hitting the twitter API. Don't do it on every page load, cache the results with a transient. Commented Mar 10, 2012 at 18:18
11

There are several caching mechanisms in WordPress and their mechanics differ, depending on choice of object cache (native or not):

+-----------+-------------------------+---------------------+
|           |         Native          | Object cache plugin |
+-----------+-------------------------+---------------------+
| Option    | Persistent (database)   | N/A                 |
| Transient | Persistent (database)   | Persistent (varies) |
| Cache     | Non-persistent (memory) | Persistent (varies) |
+-----------+-------------------------+---------------------+

In a nutshell what this means is that transient is always persistent (will survive between page loads unlike Cache natively), but it will make use of customized storage if provided (unlike Options).

This makes transients most versatile choice for caching.

However with flexibility comes undercurrent complexity and there are quite a few of nuances with them (such as limit on name length, different behavior with and without expiration, lack of garbage collection) that make them more complex than they seem.

Overall:

  • use Options for saving things that must be persistent
  • use Transients for caching anything else
  • use Cache when you have very good grasp of all three and know that Cache fits use case better than others (which won't be often)
4

I think the code from Sterling could be improved by not call the get_transient function twice. Instead store the first result in a temporary variable. Because the idea behind the Transient API ist speed ;-)

private function _get_data( $query) {
    $result = get_transient( $query );
    if ( $result ) {
       return $result;
    } else { 
       return $this->_get_query( $query ); 
    }
}
3

Short Answer: You should use it when/where you can.

Long Answer:

The Transients API is for caching. So you want to use it as much as you can. You can write a function that does this for you.

It's not overkill and if done properly ends up being pretty elegant:

// If the transient isn't false, then you can just get the cached version.
// If not, we'll call another function that runs a database query.
private function _get_data( $query) {
    return
    ( get_transient( $query ) ) ?
    get_transient( $query ) :
    $this->_get_query( $query );
}

// After you make the query, set the transient so you can leverage on caching plugins.
private function _get_query( $query ) {
  // Database logic. Results go to $results.
  ...
  set_transient( $query, $results , 0 ); // 0 Means cache for as long as you can.
}
3

Transients API is really useful when you're fetching data from external sources like Facebook, Twitter.

To get more clear idea of what Transients API is and what is difference with Cache WordPress function, I recommend to watch Otto's and Nanic's talk from WordCamp San Francisco 2011

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.