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
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    Digg-Style Dynamic Share Widget Using the Dojo Toolkit

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

  • By
    Create a Dynamic Flickr Image Search with the Dojo Toolkit

    The Dojo Toolkit is a treasure chest of great JavaScript classes.  You can find basic JavaScript functionality classes for AJAX, node manipulation, animations, and the like within Dojo.  You can find elegant, functional UI widgets like DropDown Menus, tabbed interfaces, and form element replacements within...

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!