Advertisement
Scaling & Caching

Working With Changing, Cacheable Data? WP-Transients Has Got Your Back!

by

Changing data is ever present in a WordPress-powered site. Be it a new post, an added comment, or an updated tweet for the front page, the status of a blog is constantly being altered. Since such data is often useful to a visitor, there comes a need to display it. This, however, is plagued by the fact that dynamic information—especially from external sources—results in slower, burdened sites. When it comes to these issues, the WordPress Transient API has you covered with its robust caching. The following screencast will demonstrate exactly how to take advantage of its features.


Screencast


Step 1 Get the Transient by Name

Access a transient by passing a name to the get_transient() function. Don't worry; even though you haven't created one yet, this is still the first step! For our example, we'll be getting a Twitter follower count.

$transient = 'wptuts_twitter_follower_count';
$count = get_transient( $transient );

Step 2 Check if the Transient Has Expired

If the return value of get_transient() is false, the transient has either expired (based on the time parameter explained in step 3) or was never set in the first place:

if( false === $count )
{
    // transient has expired; proceed to step 3
}

Step 3 If Expired, Set the Transient

Expired

When a transient expires, it needs to be reset; in other words, the data needs to be refreshed. To accomplish this, first retrieve the new data. In this case, get the follower count via the Twitter API as per the video:

$data = @file_get_contents( 'http://api.twitter.com/1/users/lookup.json?screen_name=envatowp' );
$json = json_decode( $data );

if( $json && isset( $json[0] ) && isset( $json[0]->followers_count ) )
    $count = (int) $json[0]->followers_count;

Now store it by using the set_transient() function. Note that this requires a name, value, and expiry time in seconds as parameters. After this time has elapsed, get_transient() will return false and trigger this step again:

// expire in 1 day ( 60 seconds/minute * 60 minutes/hour * 24 hours/day = seconds/day )
set_transient( $transient, $count, 24 * 60 * 60 );

Step 4 Put it All Together

Together

Combining this into one function yields:

function twitter_follower_count()
{
    $transient = 'wptuts_twitter_follower_count';
    $count = get_transient( $transient );

    if( false === $count )
    {
        $data = @file_get_contents( 'http://api.twitter.com/1/users/lookup.json?screen_name=envatowp' );
        $json = json_decode( $data );

        if( $json && isset( $json[0] ) && isset( $json[0]->followers_count ) )
        {
            $count = (int) $json[0]->followers_count;
            set_transient( $transient, $count, 24 * 60 * 60 );
        }
    }

    return $count;
}

The follower count not only is available for use anywhere on your site, but it is also efficiently cached each day to curb load times.


Final Code From the Video

The final code from the screencast—with a few modifications—is included below:

class WPTuts_Transients
{

    public function WPTuts_Transients()
    {

    }

    public function twitter_follower_count()
    {
        return $this->process_transient( 'wptuts_twitter_follower_count', array( $this,
            'refresh_twitter_follower_count' ), 24 * 60 * 60 );
    }

    public function refresh_twitter_follower_count()
    {
        $data = @file_get_contents( 'http://api.twitter.com/1/users/lookup.json?screen_name=envatowp' );
        $json = json_decode( $data );

        $count = false;
        if( $json && isset( $json[0] ) && isset( $json[0]->followers_count ) )
            $count = (int) $json[0]->followers_count;

        return $count;
    }

    public function twitter_recent_tweets()
    {
        return $this->process_transient( 'wptuts_twitter_recent_tweets', array( $this,
            'refresh_twitter_recent_tweets' ), 24 * 60 * 60 );
    }

    public function refresh_twitter_recent_tweets()
    {
        $data = @file_get_contents( "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=envatowp&count=5&trim_user=true&exclude_replies=true" );
        $json = json_decode( $data );

        $tweets = false;
        if( $json )
            $tweets = $json;

        return $tweets;
    }

    public function feedburner_subscriber_count()
    {
        return $this->process_transient( 'wptuts_feedburner_subscriber_count', array( $this,
            'refresh_feedburner_subscriber_count' ), 24 * 60 * 60 );
    }

    public function refresh_feedburner_subscriber_count()
    {
        $data = @file_get_contents( 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=wptuts' );

        $count = false;
        if( preg_match( '~circulation="(\d+)"~', $data, $matches )
                && isset( $matches[1] ) )
            $count = (int) $matches[1];

        return $count;
    }

    private function process_transient( $transient, $refresh, $time )
    {
        $data = get_transient( $transient );

        if( false === $data )
        {
            if( is_callable( $refresh ) )
            {
                $data = call_user_func( $refresh );
                set_transient( $transient, $data, $time );
            }
        }

        return $data;
    }

}

Thanks

I hope that this has been helpful tutorial on the WordPress Transient API. Please feel free to leave your comments below.

Related Posts