How can ColdFusion cache a database query?
ColdFusion query caching is used to keep frequently accessed query results in memory rather then having to pull the results from the database again and again.
Cached queries work by using either the cachedAfter or the cachedWithin attributes of the <cfquery> tag. In order to use either form of cached queries, query caching must be enables in the ColdFusion administrator.
The cachedAfter attribute is used to cache a query after a certain date has passed by passing in a specific date.
<cfquery name="qAfterTest" datasource="myDs" cachedAfter="10-10-2005"> select name from recipes </cfquery>
The cachedWithin attribute is used to cache a query within a specified date/time range by passing in a valid data/time range.
<cfquery name="qWithinTest" datasource="myDs" cachedWithin="#createTimeSpan(0, 5, 0, 0)#"> select name from recipes </cfquery>
It is important to note that cached queries are identified by the exact SQL code and <cfquery> tag attributes (datasource, name, etc.) used in the <cfquery> tag call that created them. The only exceptions to this rule are the cachedAfter and the cachedWithin attributes themselves can be altered without effecting the identification of a cached query.
This question was written by Jeremy Petersen
It was last updated on March 8, 2006.