ColdFusion: Cache Yahoo! Web Service REST Calls

Caching is essential to decrease network loads and improve overall reliability of your web applications. In this HOWTO, we demonstrate simple caching techniques that are both fast and reliable.

Caching with Application Variables

ColdFusion contains a simple folder based application sytem. Any folder that contains an Application.cfc or Application.cfm file (or a subfolder of) can be considered a ColdFusion application. A ColdFusion application has access to multiple persistent scopes:

The Application scope contains variables available to all users of an application. It is typically used to cache the data that everyone needs. Our examples will use this scope.

The Session scope contains variables that are unique per user. A session is defined in ColdFusion as one browser that hits a site and continues to hit the site within a certain timeout period. If you need to cache data and have it be unique per user, this is the scope you would use. All of our examples will use the Application scope, but they could easily use the Session scope as well. Just keep in mind though that every user will have it's own data. This could impact your system performance if you put too much data in the scope.

Lastly there is a Client scope as well. This scope is also unique to the user but it does not timeout. It can persist from session to session. This scope does not accept complex variables however, and in general is not used very often.

To begin our example, we need to create an Application. As mentioned above, this can be done with either an Application.cfc or Application.cfm file. It can also be done by simply putting a <cfapplication> tag on your page. While this is not normally done, it does make for a simple example and is how this example will be created. So the first line of our code is:

<cfapplication name="yahooCacheExample1">

When ColdFusion encounters this line, it considers the request to be in the yahooCacheExample1 application. Any Application variable created will be unique to this application. Next we will test to see if our data has already been created:

<cfif not isDefined("application.results")>

The isDefined() function does exactly what it says - checks to see if a variable exists. In this case we are seeing if the Application variable "results" is defined. If it isn't, we need to download the data:

<cfset requestURL = "http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Madonna&results=4">
<cfhttp url="#requestURL#" charset="utf-8">

To cache the data, we can simply store it in the Application scope.

<cfset xmlData = xmlParse(cfhttp.fileContent)>
<cfset application.results = xmlData>

At this point we now have a cached version of the XML data in the Application scope. To loop over it we can use the same code we had used before, just with a new variable (application.results). The HTTP call and XML parsing will be done once per the lifetime of the Application.

A complete code sample is available here.

Caching with Application Variables and a Timeout

In the previous example, the Application scope was used to cache the result of the Yahoo web service call. This works easily enough, but will persist until the Application scope expires. If the application is contanstly hit during the day, the scope will not expire. What if you want to cache the data but only for a certain amount of time? One solution is to simply store the time when the cache was created:

<cfset application.cachecreated = now()>

Once you have this value, you can compare it to the current time using the dateDiff() function. The following code checks to see if the application.cachecreated variable was defined 20 minutes or more in the past:

dateDiff("n", application.cachecreated, now()) gt 20

A complete code sample is available here.

For More Information

For more information on using ColdFusion with Yahoo! Web Services APIs, see The Yahoo! Developer Network ColdFusion Developer Center.