Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a Better ColdFusion Cache Tag : Page 2

ColdFusion's <cfcache> tag is great, but it caches only whole HTML pages and most templating systems won't play along. Learn how to develop a custom tag that allows you to cache arbitrary sections of ColdFusion-generated pages.


advertisement
Verify Values
Your first step is to check for each of the inputs in Table 1 and supply some default values for the ones that are missing. To do this, use the <cfparam> tag:

<cfparam name="Attributes.key" default="#CGI.script_name##CGI.query_string#"> <cfparam name="Attributes.expiration" default="1,h">

If the name is missing, compose the tag name from the script name and the query string. For the expiration, use the same syntax used in ColdFusion datetime functions (which means that 'm' means "months" not "minutes"—'n' indicates minutes, so be careful). By default, the expiration is set to "1,h", which translates to one hour.



The next attribute tells ColdFusion where to cache the data:

<cfparam name="Attributes.scope" default="application">

By default, the cache is for the whole application, though you can also make the cache unique to a server ("server") or even to an individual client ("session"). Setting the scope to "session" should be done with caution, however, as storing lots of data in the session scope can consume lots of memory. Do it only when it is safe and the tradeoffs are clear.

To simplify the code, create a pointer to the given scope and parse out the expiration information:

<cfset ptr = StructGet(theScope)> <cfset num = ListFirst(Attributes.expiration)> <cfset datePart = ListLast(Attributes.expiration)>

As for the actual data, store it in a ColdFusion struct, with the various items in the cache keyed to the "key" parameter above, and store the expiration data in a second struct. First, check whether these structs exist, and create them if not:

<cfif not StructKeyExists(ptr,"CacheOMatic")> <cfset ptr["CacheOMatic"] = StructNew()> </cfif> <cfif not StructKeyExists(ptr,"CacheOMaticDateTime")> <cfset ptr["CacheOMaticDateTime"] = StructNew()> </cfif>

Now you do the actual caching work. The code is actually quite short. The outer if/then structure, which can be a bit confusing, looks like this:

<cfif thisTag.executionMode is "start"> … <cfelse> <cfset ptr.CacheOMatic[theKey] = thisTag.GeneratedContent> <cfset ptr. CacheOMaticDateTime[theKey] = Now()> </cfif>

Custom Tags execute twice: once for the starting tag and once for the ending tag. This structure executes the top set of statements when the first tag is read (<cf_CacheOMatic>), then executes the final statements when reading the end tag (</cf_CacheOMatic>). So at the end of each tag execution, you save the "stuff" inside the tag to the data struct and save an expiration for it to the expiration date struct.

Now the code inside the first tag is where you actually check to see if you should use cached data or not:

<!---If we have data in the cache for the key---> <cfif StructKeyExists(ptr.CacheOMatic,Attributes.key)> <!---Get the expiration date for the data---> <cfset dateCreated = ptr.CacheOMaticDateTime[Attributes.key]> <cfset maxDate = DateAdd(datePart,num,dateCreated)> <!---If the expiration date is greater than the current date ---> <cfif DateCompare(maxDate,Now()) EQ 1> <cfoutput>#ptr.CacheOMatic[Attributes.key]#</cfoutput> <cfexit> </cfif> </cfif>

This algorithm is fairly straightforward, and it goes like this:

If you have data in the cache for the key
Get the expiration date for the data
If the expiration date is greater than the current date
Output the data in the cache and exit this tag

Note that if you exit the tag by calling <cfexit>, the code inside the CacheOMatic tag will never run. This is the work you save if the cached data is not stale. If the data is stale, you allow the whole tag to execute.

To test the tag, create a minimal ColdFusion script as follows: <cfapplication />
<cf_CacheOMatic key="date1" expiration="20,s"> <cfoutput>#Now()#</cfoutput></cf_CacheOMatic>
<cf_CacheOMatic key="date2" expiration="1,n"> <cfoutput>#Now()#</cfoutput></cf_CacheOMatic>

When you hit this page with your browser, you should see a page with two dates. If you keep refreshing, the first date will update after 20 seconds (hence, the expiration "20,s"), while the second date updates after one minute ("1,n"). Your cache tag is complete.

A Simple Performance Boost
Not all scripts can be cached, of course. For expensive database transaction code or other time-consuming ColdFusion code that does not need to be executed with every request, however, you now have an elegant and simple solution. Not bad for about 30 lines of code.



Eric Jansson is Assistant Director of Systems and Development for the ACS Technology Center, a regional center supporting the development and use of technology in higher education. His work has focused on commercial and open-source application development in the field of secondary and post-secondary education.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap