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

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Incredible Demos

  • By
    MooTools Zoomer Plugin

    I love to look around the MooTools Forge. As someone that creates lots of plugins, I get a lot of joy out of seeing what other developers are creating and possibly even how I could improve them. One great plugin I've found is...

  • By
    Create a 3D Panorama Image with A-Frame

    In the five years I've been at Mozilla I've seen some awesome projects.  Some of them very popular, some of them very niche, but none of them has inspired me the way the MozVR team's work with WebVR and A-Frame project have. A-Frame is a community project...

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!