Simple ColdFusion Feed Caching Manager

ColdFusion 8 added the cffeed tag, a great and simple utility for fetching and parsing rss and atom feeds. It’s a great and simple to use utility, but there are a couple of issues to keep in mind.

Every time you make a “read” call to an external source, you’re preforming an http request. Most feeds that I’ve pulled in aren’t updated more than a few times per week. Just using cffeed, even a modest hit count can quickly add up to thousands of unnecessary requests. Aside from the bandwidth, you’re fully at the mercy of that external server. Each request is potentially slow, invalid, or (ahem, Twitter) otherwise unavailable.

You can totally bypass these problems by simply caching a copy of the feed to diske, and then quickly and safely parsing that file as needed. If you can grab it once, then you’ll always have that information quickly available. I wrote a lightweight utility for doing just this. It’s simple, but it’s made my life so much easier that I thought I’d share.

First create and instantiate the feed manager.

  feedManager = createObject( "component", "feedManager" );
  feedManager.init( folder = absolutePathToSaveFeedFiles );

Then add the feeds, with unique keys for retrieval.

  feedManager.addFeedURL(
    key = "BLOG",
    url = "http://joezack.com/index.php/feed"
  );

  feedManager.addFeedURL(
    key = "CNN",
    url = "http://cnn.com/feed"
  );

Fetch and save the feeds to disk, if available and valid. I run this in a scheduled task, every hour or so.

  feedManager.cacheFeeds();

  // or to just cache one feed
  feedManager.cacheFeed("CNN");

Finally, to retrieve the cffeed parsed file, just call the getFeed method! So long as there has ever been a feed successfully retrieved, you’ll have data.

  // Request feed from disk.
  feed = feedManager.getFeed(key = "BLOG");

Download the file!