markjaquith / WP-TLC-Transients

GNU General Public License v2.0
341 stars 37 forks source link

Check for Transient before Running TLC Transients? #5

Closed adamcapriola closed 12 years ago

adamcapriola commented 12 years ago

Yoooo,

I run a database type of website that has a lot of entries (like 5,000 at the moment) and I use TLC Transients for every one of them (and it kicks ass!).

But my CPU usage has gone way up since I started using it - my host says I'm getting a ton of POST request for http://mysite.com/?tlc_transients_request.

I assume that's exactly what's supposed to happen, but I was thinking of checking for a stored transient BEFORE running TLC Transients in my code to save on CPU usage. Is this a good idea, or does it defeat the purpose of it? This is what my function now looks like:

//
// Transient Function (better than just saving transient value)
//
function tlc_price_scraper( $postID = '', $name = '', $set = '', $number = '' ) {

    $tlc_name = $postID . '-price'; // Name of value in database.
    $cacheTime = 24; // Time in hours between updates.

    if ( false === ( $t = get_transient($tlc_name) ) ) {

        $t = tlc_transient( $tlc_name )
        ->updates_with( 'price_scraper', array( $postID, $name, $set, $number ) )
        //->expires_in( 5 ) // testing
        ->expires_in( 60 * 60 * $cacheTime ) // live
        //->background_only()
        ->get();

        return $t;
    }
    else {
        return $t[1];
    }
}

So I basically just check with get_transient before running tlc_transient. Good idea? Or not?

aaroncampbell commented 12 years ago

That URL should only be called for a background fetch, and that should only happen when the transient doesn't exist or has soft-expired (is out of date, but still exists). If you want fewer of those, just increase your expires time so your transients last longer.

adamcapriola commented 12 years ago

Good point man, I actually looked through the code a little more a week or so ago and realized the same thing. I adjusted my expires times and things are straight now.