ColdFusion Caching

<cfsavecontent> tag

The <cfsavecontent> tag is an suitable way to cache the results of a block of ColdFusion code. To use the tag it requires a single attribute ‘variable’ declared in the opening tag. You can call this variable later between <cfoutput> tags in order to display the results of the block.

Here is a short example of how it works:

<cfsavecontent variable='displayLater'>
<cfset rand1 =ToString(RandRange(0,1000000))>
<cfset combo = 'Robert' & rand1 >
<cfoutput> #combo#</cfoutput>
</cfsavecontent>
<br>
<hr>
<cfoutput>#displayLater#</cfoutput>

This creates a page that displays the answer after a horizontal rule.

You can then replace substring’s in this content if it is creating a template for you that you plan to use several times.

<cfoutput> #ReplaceNoCase("#displayLater#","Robert","Ringo")# </cfoutput>

This takes arguments of the string you want to replace parts of and 2 substrings, the first one is what you want to replace and the second is what you want to replace it with.

#ReplaceNoCase('string', 'substring1', 'substring2')

The results of this will look like this: