Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to store some Twitter API data in WordPress. After every hour, I want to fetch new data from Twitter, and update only if the data is successfully retrieved from API (sometimes the API gives error, so in that case I want to keep using the old data). So in that case should I use set_transient or update_option?

If I used update_option then at least I can check if new data is available before updating the option.

What happens if I set_transient for an hour and after one hour the new data is not available, will I lose the stored data?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

A transient is not like a wp_cron job in that they don't update themselves when a set time has elapsed. A transient will update when an attempt is made to access the data. If you actually do need an approximately hourly update you will need to use wp_cron, though in practice it may not matter much and you could use a wp_cron job to update the transient if you wanted.

But to answer the question asked, when you run get_transient to check your transient value it will return false if "the transient does not exist, does not have a value, or has expired", but you don't know which, and running get_transient also runs delete_option if the timeout has expired. I verified that the transient is in fact deleted from the database by setting up a 60 second timeout test, and checking the database itself for the transient.

Transients do have an advantage over normal options in terms of caching.

Also of note is that Transients are inherently sped up by caching plugins, where normal Options are not. A memcached plugin, for example, would make WordPress store transient values in fast memory instead of in the database. For this reason, transients should be used to store any data that is expected to expire, or which can expire at any time.

http://codex.wordpress.org/Transients_API

This may not matter on your site in particular, but in general it does count toward using the Transients API. You will have to do something about the lost data issue, though. Something like...

function get_twit_wpse_94911() {
  $trans = 'test_transient';
  $data = get_option('_transient_'.$trans);
  if (empty($data)) {
    $data = 'Yay Awesome Data'; // default data if you want it
  }
  if (!get_transient($trans)) {
    // check for new information
    // $new = fetch_twitter_however_you_are_doing_it();
    // check for the integrity of the new data
    // !empty() may not be adequate
    if (!empty($new)) {
      $data = $new;
    }
    set_transient($trans,$data,60*60);
  }
  return get_transient($trans);
}
share|improve this answer
    
The TL;DR here is use the Transients API for things that are intended to expire. Note: for external API data as (ahem) difficult to work with as the Twitter API, I have had luck setting two transients: a short-term transient (1 hour) and a long-term transient (24 hours). The long-term transient is the fallback for the short-term transient. If the third-party API can't return valid data once every 24 hours, then there's not much WordPress can do about it. The API is probably just crappy (looking at you, Twitter). –  Chip Bennett Apr 7 '13 at 20:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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