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.
<cfoutput>
The current date/time is: #Now()#
</cfoutput>
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.
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 5013 times and has 1 comments.
13 related blog entries
- 14 Days of ColdFusion 9 Caching: Day 14 - Setting the Server Cache Properties (November 30, 2009)
- 14 Days of ColdFusion 9 Caching: Day 13 - Retrieving the Server Cache Properties (November 29, 2009)
- 14 Days of ColdFusion 9 Caching: Day 12 - Removing All Items in Cache (November 28, 2009)
- 14 Days of ColdFusion 9 Caching: Day 11 - Reporting On All Items in Cache (November 27, 2009)
- 14 Days of ColdFusion 9 Caching: Day 10 - Session-Specific Caching (November 26, 2009)
- 14 Days of ColdFusion 9 Caching: Day 9 - Dependent Caching (November 25, 2009)
- 14 Days of ColdFusion 9 Caching: Day 8 - Cache Hits and Cache Misses (November 24, 2009)
- 14 Days of ColdFusion 9 Caching: Day 7 - Using cacheRemove to Flush Items from Cache (November 23, 2009)
- 14 Days of ColdFusion 9 Caching: Day 6 - Using cachePut, cacheGet, and cacheGetMetadata (November 22, 2009)
- 14 Days of ColdFusion 9 Caching: Day 5 - The Difference Between Timespan and Idletime (November 21, 2009)
- 14 Days of ColdFusion 9 Caching: Day 4 - Flushing a Template From Cache (November 20, 2009)
- 14 Days of ColdFusion 9 Caching: Day 3 - Viewing Page Cache Metadata (November 19, 2009)
- 14 Days of ColdFusion 9 Caching: Day 2 - Caching a Page Fragment (November 18, 2009)


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