User Guide

Table Of Contents
Optimizing ColdFusion applications 301
You can use the cfsavecontent tag to cache infrequently changing output in a shared scope
variable. If the information is used throughout the application, save the output in the Application
scope. If the information is client-specific, use the Session scope. Because of the overhead of
locking shared scope variables, use this technique only if the processing overhead of generating
the output is substantial.
Before you use this technique, also consider whether other techniques are more appropriate. For
example, query caching eliminates the need to repeat a common query. However, if the effort of
processing the data or formatting the output is substantial, using the
cfsavecontent tag can save
processing time.
Using this technique, if the variable exists, the page uses the cached output. If the variable does
not exist, the page gets the data, generates the output, and saves the results to the shared scope
variable.
The following example shows this technique. It has two parts. The first part welcomes the user
and prints out a random lucky number. This part runs and produces a different number each
time a user opens the page. The second part performs a database query to get information that
changes infrequently; in this example, a listing of the current special sale items. It uses the
cfsavecontent tag to get the data only when needed.
Tip: If you use this technique frequently, consider incorporating it in a custom CFML tag.
<!--- Greet the user. --->
<cfoutput>
Welcome to our home page.<br>
The time is #TimeFormat(Now())#.<br>
Your lucky number is: #RandRange(1,1000)#<br>
<hr><br>
</cfoutput>
<!--- Set a flag to indicate whether the Application scope variable exists.--->
<cflock scope="application" timeout="20" type="readonly">
<cfset IsCached = Not IsDefined("Application.ProductCache")>
</cflock>
<!--- If the flag is false, query the DB, and save an image of
the results output to a variable. --->
<cfif not IsCached>
<cfsavecontent variable="ProductCache">
<!--- Perform database query. --->
<cfquery dataSource="ProductInfo" name="specialQuery">
SELECT ItemName, Item_link, Description, BasePrice
FROM SaleProducts
</cfquery>
<!--- Calculate sale price and display the results. --->
<h2>Check out the following specials</h2>
<table>
<cfoutput query="specialQuery">
<cfset salePrice= BasePrice * .8>
<tr>
<td>#ItemNAme#</td>
<td>#Item_Link#</td>