How can I cache the results of a block of ColdFusion code?

The ColdFusion <cfsavecontent> tag is a convenient way to store the results of a block of ColdFusion code. The <cfsavecontent> tag is called with both a beginning <cfsavecontent> and an ending </cfsavecontent> tag. <cfsavecontent> has a single required attribute called variable. This attribute will hold the cached results of the <cfsavecontent> tag.

So now that you know how to call the <cfsavecontent> tag, what does it do? When you wrap a block of ColdFusion code with a <cfsavecontent> tag, the block of code executes, but any output that is generated by the block of code doesn't display to the screen. Instead, the output is stored in the variable that you choose in the varaible attribute of the tag.

<cfsavecontent variable="cachedOutput">
  I am going to count to 10!<p>
  <cfloop index="loopOn" from="1" to="10">
	  <cfoutput>#loopOn#<br></cfoutput>
  </cfloop>
</cfsavecontent>

If you want to see the output, you would need to do the following:

<cfoutput>#cachedOutput#</cfoutput>

This question was written by Jeremy Petersen
It was last updated on February 8, 2006.

Categories

Caching

Comments

comments powered by Disqus