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

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?