Prabha Vathi
Prabha Vathi

Reputation: 347

Abraham - TwitterOAuth : Using with php cacher

Before i make a call to twitter, I check for the chache version of data. If there is no cache, then i create a connection with twitter.

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token);

This is how i have planned to do. I haven't started it yet.
The question is :

Which is best way to do this?

1) Check for the cache, if there is no cache create a new connection and get details from twitter.
2) Create a new connection in top of every file ( or in a header ) check for cache, if there is no cache, (connection is already exists. so) get the details from twitter.

And, How do i check whether the connection ($connection) is active?

Upvotes: 3

Views: 341

Answers (1)

Bas Kuis
Bas Kuis

Reputation: 770

The below would roughly do what you're asking for. The example below uses APC

//define cache key
$cacheKey = "twitterResponse[" . md5($endpoint, implode("&", $parameters) . "]";

//attempt to grab from cache
$foundCachedResponse = false;
$twitterResponse = apc_fetch($cacheKey, $foundCachedResponse);

//only when needed
if(!foundCachedResponse){

    //twitter connection
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token);

    //make the call
    $twitterResponse = $connection->getTweetsOrWhatever($parameters);

    //cache the response
    apc_store($cacheKey, $twitterResponse, 600);

}

//return 
return $twitterResponse;

To see if APC is enabled:

if(!extension_loaded('apc')){ 
    die('no APC');
}

Upvotes: 0

Related Questions