dongmo8943 2013-09-04 17:16
浏览 23
已采纳

亚伯拉罕 - TwitterOAuth:使用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?

  • 写回答

1条回答 默认 最新

  • doula2426 2013-09-23 18:25
    关注

    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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?