A cached query is a query that has its results stored in the server's memory. The results are stored when the query is first run. From then on, whenever that query is requested again, ColdFusion will retrieve the results from memory.
You can cache a query using the cachedAfter
attribute. If the query was last run after the supplied date, cached data is used. Otherwise the query is re-run.
<cfquery datasource="Entertainment" cachedAfter="July 20, 2016">
select *
from Movies
</cfquery>
In order for the cache to be used, and multiple calls to the database be avoided the current query must use the same SQL statement, data source, query name, user name, and password as the cached query used. This includes whitespace in the query.
As such the following queries create different caches, even though the trimmed characters are the same and the query results are identical:
<cfquery datasource="Entertainment" cachedAfter="July 20, 2016">
select *
from Movies
<cfif false>
where 1 = 1
</cfif>
<cfif true>
where 1 = 1
</cfif>
</cfquery>
<cfquery datasource="Entertainment" cachedAfter="July 20, 2016">
select *
from Movies
<cfif true>
where 1 = 1
</cfif>
<cfif false>
where 1 = 1
</cfif>
</cfquery>