One of my favorite features in ColdFusion 9 is Ehcache and all the new caching functions. Understanding caching on any application server is a fundamental skill of an effective Web developer. Why? Because today's applications go well beyond simple brochure sites, employee address books, and the like. Most applications start small which is perfectly natural. As a developer it's your job to think bigger and plan for your application being really large. The first incarnation of Twitter was built in two weeks and I'm certain the developers had no idea it would be as big as it is today. And while Twitter is not the shining of example of how to design scalability from day one, they're certainly a case of something becoming infinitely larger than planned.

Over the next two weeks I'll bring you simple, easy to understand examples of caching in ColdFusion 9. The examples won't demonstrate real world usage of caching (for the most part), but they will iterate the new caching functionality in CF9 using the <cfcache> tag as well as the new caching functions. At the bottom of each post will be a link you can press to download all the example code in one fell swoop. Included in the download are my slides and walkthrough instructions from my lab at BFusion 2009. You only need to download the code once; the link will be the same in each daily post. One final note and suggestion: read through Rob Brooks-Bilson's series on caching in CF9 which starts here. Rob explains in great detail why you should cache and describes architectural considerations you need to be aware of.

So, let's kick off this series with the first example.

<cfcache action="cache" timespan="#CreateTimeSpan(0,0,0,5)#">

The current date/time is: #Now()#

Using the <cfcache> tag at the top of a CFML template allows you to cache an entire page on the ColdFusion server. The default value for the action attribute is serverCache which results in a cached page being stored on the server only. In this example I show the use of the action attribute being set to cache which results in both the server and the client receiving the stored page. The timespan attribute can be set to the result of the CreateTimeSpan() function as shown here, or you can set it to a decimal number of days such as 5 (5 days), 1.5 (1.5 days) or .25 (1/4 day). Regardless of the method you choose, the timespan attribute determines how long an item remains in the cache (JVM - Java Virtual Machine).

There are many more attributes of the <cfcache> tag, some we'll look at in later posts. This example demonstrates the bare minimum you'll probably find yourself using.

Click here to download the code mentioned in this post.

Aaron West's Gravatar
About this post:

This entry was posted by Aaron West on November 17, 2009 at 8:00 AM. It was filed in the following categories: ColdFusion. It has been viewed 29540 times and has 1 comments.

13 related blog entries

1 Responses to 14 Days of ColdFusion 9 Caching: Day 1 - Caching a Full Page

  1. I have not used too much caching in my life, so I am really looking forward to your posts.