The
tag saves the results of processing the tag body in a variable. For example, if
cfsavecontent
the body of the
cfsavecontent
that displays data, the variable saves the output.
You can use the
cfsavecontent
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 in formatting the output is substantial, using the
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 case a listing of the current special sale items. It uses the
tag to get the data only when needed.
cfsavecontent
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>
<td>#Description#</td>
<td>#salePrice#</td>
tag contains a
cfexecute
tag to cache infrequently changing output in a shared scope
tag that runs an executable program
cfsavecontent
Optimizing ColdFusion applications
tag can
279
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers