Retrieve, Cache, and Display Your FeedBurner Subscriber Count

By  on  

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.

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.

Recent Features

Incredible Demos

  • By
    Spatial Navigation

    Spatial navigation is the ability to navigate to focusable elements based on their position in a given space.  Spatial navigation is a must when your site or app must respond to arrow keys, a perfect example being a television with directional pad remote.  Firefox OS TV apps are simply...

  • By
    Facebook Sliders With Mootools and CSS

    One of the great parts of being a developer that uses Facebook is that I can get some great ideas for progressive website enhancement. Facebook incorporates many advanced JavaScript and AJAX features: photo loads by left and right arrow, dropdown menus, modal windows, and...

Discussion

  1. It may be only a small concern (and an “undocumented feature” for some users), but this function results in a new text file being created for every day it is triggered. Whilst alot of webhosts give very generous file storage quotas, and a feed’s popularity could be determined over time using these files, it seems a little wasteful.

    Would another option be to have a single text file (“feedburner_sub_count.txt”) and save into that file a serialized array containing two key-value sets keyed as “date” and “count”?

    Of course, this would mean that the file would have to be loaded and unserialized to get the “date” value and then figure out whether it needed to pull data from Feedburner again (to replace the array values and then serialize and resave the new array in the same file), but it would mean only one file.

  2. I hear you Luke. I prefer separate files for the sake of simplicity but your idea is a good one for those that would like to stick to one file.

  3. I know not everyone can do this, but I actually run a cron job on my server that replaces the feed count every 2 hours or so. The difference there is I make a direct replacement in my wordpress theme instead of using a text file.

  4. Nathan barnett

    It makes sense to me to use a simple library like Cache_Lite from PEAR to handle this sort of problem. The nice thing about using Cache_Lite is that you can set an expiration time and have the natural flow of traffic decide when to repopulate the cache. It can also be automatically serialized if your saving arrays and the like. In a high traffic environment its often important to set the cache expiration to never expire and re-populate the cache via cron. I personally use a simple wrapper to use either Cache_Lite or memcached so I can easily switch between the two caching methods depending on what works best for the specific problem.

    http://pear.php.net/package/Cache_Lite
    http://us3.php.net/memcache

  5. Memcache > cache files

  6. @Luke: Instead of storing the date inside the file, why not just use the time when the file was modified?

    $interval = 24 * 60 * 60;  // 24 hr * 60 min / hr * 60 sec / min
    if( file_exists( $path_to_file ) && ( time() - $interval ) < filemtime( $path_to_file ) ) {
         readfile( $path_to_file );
         exit();
    }
    
    // find new information and store it in the file
    
  7. Luke

    @Karthik: Even better! Hadn’t thought of that option myself, but, I must say, a very elegant solution.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!