Visit The XML Pro News Directory
CSS
Templates, Tag Reference
News
Articles, Books
XML Articles
Blogs, Recent News
XML Consultants
Consulting Networks, Training
XML Editors
XML Text Editors, XML WYSIWYG Editors
XML Encoding
Tags, Rules
XML Layouts
Code Layout, Page Layout
XML Programming
Methods, Applications

Submit your site for FREE

Caching CFCONTENT


Raymond Camden By: Raymond Camden

A few days ago I blogged about a code review I was doing for another client. Yesterday I found another interesting bug in their code. (It is always easier to find bugs in other people’s code.)

The code in question was an API tester for their web site. The page had a nice set of links to let you test all of their XML based responses for their site. I noticed something odd in one test. The first time I ran the test, I got a nicely formatted XML result in the browser. The second time however I got an odd jumble of text.

Viewing the source revealed that the XML was there ok. I took a look at the code and spotted the problem right away. See if you can as well:

<cf_ScopeCache
cachename="foo_#somevar#"
scope="server"
timeout="1800">

<cfset myXML = application.myGateway.getMyXML(args)/>

<CFCONTENT TYPE=”text/xml” RESET=”Yes”>
<CFOUTPUT>#ToString(myXML)#</CFOUTPUT>
</cf_scopecache>

If you don’t recognize cf_scopecache, it is just a caching custom tag. It takes the text result of the stuff inside it and stores it.

And that’s where our problem is. Our caching tag caches the text. But the CFCONTENT tag is a Request specific tag. By that I mean it changes the current request. The first time the page is hit, it will execute, as will all the code inside the scopeCache custom tag. The second time though only the text is returned. CFCONTENT isn’t run. The fix was simple - move the CFCONTENT:

<CFCONTENT TYPE="text/xml" RESET="Yes">
<cf_ScopeCache
cachename="foo_#somevar#"
scope="server"
timeout="1800">

<cfset myXML = application.myGateway.getMyXML(args)/>

<CFOUTPUT>#ToString(myXML)#</CFOUTPUT>
</cf_scopecache>

The same issue would exist if you used CFCACHE on top of the page.

About The Author

Raymond Camden is Vice President of Technology for roundpeg, Inc. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books and is the creator of many of the most popular ColdFusion community web sites. He is an Adobe Community Expert, user group manager, and the proud father of three little bundles of joy.

Leave a Reply