I’m doing a code review for a client (which I’ve been doing a lot lately and boy is it fun to find bugs in someone else’s code for once ;) and noticed his home page was using CFFEED to put CNN’s latest articles. However, he wasn’t doing any caching at all. I whipped up a quick mod for him to show a simple example of caching. It doesn’t have any fancy way to force a refresh, but quickly sped up his home page. So here is a quick code snippet showing simple caching of CFFEED:
<cfset feedurl = "http://www.mugglenet.com/feeds/news.xml">
<cfset cacheMinutes = 60>
<!— check if cached, and not too old —> <cfif not structKeyExists(application,”rsscache”) or dateDiff(“n”, application.rsscache.created, now()) gt cacheMinutes> <cffeed source=”#feedurl#” query=”entries”> <cfset application.rsscache = structNew()> <cfset application.rsscache.data = entries> <cfset application.rsscache.created = now()> </cfif>
<cfdump var=”#application.rsscache#”> </code>
All I do is check for an application variable. If it doesn’t exist, or the ‘created’ value for the cache is more than cacheMinutes old, I reload from cffeed. While not as fancy as a conditional get, it is certainly a lot simpler.