Friday quickie - Example of simple caching of RSS Items

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 = ""> <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 = entries> <cfset application.rsscache.created = now()> </cfif>

<cfdump var="#application.rsscache#">

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA