« Zillow.com | Automatically hide file extensions in Windows »

May 4, 2006

Clearing a cached query in ColdFusion

The ColdFusion cookbook has a couple of recipes for dealing with cached database queries. In theory it's relatively simple. You can instruct ColdFusion to cache a query for a specified amount of time using the cachedWithin attribute of the <cfquery> tag. You can clear a cached query by calling the same query again, but setting the cachedWithin attribute to a past time. But I recently discovered an interesting issue regarding clearing cached queries…

It turns out that not only must the SQL query and <cfquery> attributes be identical (with the exception of the cachedWithin attribute), but both queries must also have identical whitespace formatting. In other words, the following will not work:


<cfquery name="myQry" datasource="myDS"
cachedwithin="#createTimeSpan(0,1,0,0)#">
select *
from users
order by last_name
</cfquery>
<cfquery name="myQry" datasource="myDS"
cachedwithin="#createTimeSpan(0,0,0,-1)#">
select *
from users
order by last_name
</cfquery>
Because the 'clearing' query is indented more than the original query the cached query will not be cleared. So this is one time when whitespace really does matter.

Posted at 2:21 PM in Web Development