检索,缓存和显示您的FeedBurner订阅者计数

One way of significantly speeding up your website is by caching information you retrieve remotely from other websites or databases. Caching information is extremely simple and can save your users seconds of unnecessary waiting. Here's how I cache my FeedBurner follower count on my site.

一种显着加快网站速度的方法是缓存从其他网站或数据库远程检索的信息。 缓存信息非常简单,可以为用户节省不必要的等待时间。 这是我在网站上缓存FeedBurner关注者计数的方法。

PHP (The PHP)


/* settings */
$cache_path = '/cache/';
$rss_file_name = date('Y-m-d').'-rss.txt';

/* rss */
if(file_exists($cache_path.$rss_file_name)) {
	$rss_subcribers = file_get_contents($cache_path.$rss_file_name);
}
else {
	$rss_content = get_url('https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=dfadajf324789fhSDFDf48');
	$subscribers = get_match('/circulation="(.*)"/isU',$rss_content);
	if($subscribers) {
		$subscribers = number_format($subscribers,0,'',',');
		file_put_contents($cache_path.$rss_file_name,$subscribers);
		$rss_subcribers = $subscribers;
	}
}

/* display */
echo 'My subscriber count is: ',$rss_subscribers;

/* connects to the URL, returns content */
function get_url($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}

/* helper: does the regex */
function get_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}



The first thing we do is check to see if our cached file exists. If it does, we simply return its contents. If the cache file doesn't exist, we connect to our private FeedBurner URL, parse the content, and save the feed to a file named after the current day. Note the way we can check if we had gotten the count for the current day is by naming the file in "year-month-day" format.

我们要做的第一件事是检查我们的缓存文件是否存在。 如果是这样,我们只需返回其内容。 如果缓存文件不存在,我们将连接到私有FeedBurner URL,解析内容,并将提要保存到以当天命名的文件中。 请注意,我们可以通过以“ year-month-day”格式命名文件来检查是否已获得当天计数。

翻译自: https://davidwalsh.name/cache-feedburner

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值