Today, let's look at caching a page fragment. Here's the code:
<cfoutput>This date/time IS cached: #Now()#<br /></cfoutput>
</cfcache>
<cfoutput>This date/time is not cached: #Now()#</cfoutput>
Result in a browser:
This example shows how to cache a page fragment on both the client and the server using action="cache." Notice how the <cfcache> tag is wrapped around a simple <cfoutput> tag. The use of the opening and closing <cfcache> tag is in contrast to the example I blogged about yesterday. The date/time string within the <cfcache> block will be cached for 5 seconds while the date/time string in the <cfoutput> below that will not be cached. If you run this example on your ColdFusion 9 server the second date/time string will change every page request while the first date/time string will only change every 5 seconds.
How is this different from what was possible before ColdFusion 9? In ColdFusion 8 you could use the <cfcache> tag to cache full Web pages on the server or client. Server caching (action = serverCache) stored copies of pages on disk while client caching (clientCache) stored pages in the browsers default cache. Or you could've simply used action="cache" to store the page on both the server and the client. The example here would not work on ColdFusion 8 because page fragment caching wasn't available. You could accomplish fragment caching using a combination of <cfsavecontent> and shared scopes (Application, Server, Session, or Client) or third party utilities such as CacheBox and ScopeCache.
ColdFusion 9 adds several new features to the <cfcache> tag including memory caching (default) and page fragment caching such as the example in this post. These aren't the only additions to <cfcache>, but I'll save some of the others for later posts.
So why is page fragment caching a good thing? It allows you to isolate heavy code such as complicated queries, disk operations, ETL (extract, transform, load) tasks, etc., and cache them over time. The first user that hits the page experiences the long running task while each additional user benefits from the cached version of the operation and subsequent output (if any). It's best to limit page fragment caching to code that is universal for all users. Headers and footers of Web sites might be good things to cache while session-specific data like shopping carts would not.
One final thing to keep in mind, <cfcache> and the later function-based caching I'll post about in the future are not your only options. You can still use shared scopes such as Application and Session to strategically store, read, and output virtually any data including strings, arrays, structures, and even ColdFusion components.
Click here to download the code mentioned in this post.
About this post:
This entry was posted by Aaron West on November 18, 2009 at 8:00 AM. It was filed in the following categories: ColdFusion. It has been viewed 3168 times and has 0 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 1 - Caching a Full Page (November 17, 2009)

