缓存 FeedBurner 订阅数
上篇文章讲了如何抓取 FeedBurner 的订阅数,后来才发现,居然忘记了缓存,缓存至少有两点好处,一是减轻 google 服务器的压力,google 虽然很大方,但我们也要厚道一点,呵呵,二是可以优化博客页面的响应速度,减少停顿感。下面是采取缓存的 getRssCount 函数:
function getRssCount($rss_link) {
if (!$output = wp_cache_get($rss_link,’curl_cache’)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=’ . $rss_link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
curl_close($ch);
$result = new SimpleXMLElement($result);
$result = $result->xpath(‘//entry’);
list( , $node) = each($result);
$xml = $node->asXML();
$output = substr($xml, 38, 2);
for ($i = 38, $output = $xml[38]; $xml[++$i] != ‘”‘;) {
$output .= $xml[$i];
}
wp_cache_add($rss_link, $output, ‘curl_cache’, 86400);
}
echo $output;
}
ps:类无法用 serialize() 序列化,所以保存为了 xml。
This entry was posted on Monday, June 8th, 2009 at 7:44 am and is filed under wordpress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


