Retrieve, Cache, and Display Your FeedBurner Subscriber Count
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.
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.
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.
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.
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
Memcache > cache files
@Luke: Instead of storing the date inside the file, why not just use the time when the file was modified?
@Karthik: Even better! Hadn’t thought of that option myself, but, I must say, a very elegant solution.