As I mentioned in the introduction, ColdFusion provides a built-in query caching method. This method uses two simple settings—one that specifies the length of time to cache a query in a tag attribute, and the number of queries that can exist in the cache in the ColdFusion Administrator.
Cfquery Tag's cachedWithin and
cachedAfter AttributesThe CFQUERY tag has two optional attributes for caching
queries: cachedWithin and cachedAfter. In
my experience, developers use the cachedWithin attribute
more often than the cachedAfter parameter; the difference
between them is minimal.
The cachedWithin attribute represents a period of time
for caching a query; you specify the value with the createTimeSpan()
function. The first time ColdFusion executes the query with this
attribute, it caches and tags the query with the current date/time stamp.
After that, each call to the CFQUERY tag evaluates the
current date/time and specified time span against the cached query’s
date/time stamp. If ColdFusion compares the dates and determines that
the cached query has not expired, ColdFusion retrieves the previously
cached record from memory and makes it available in the executed page.
Of course, if the cached query has expired, then ColdFusion executes
the query again and caches the new recordset, tagging it with the current
date/time stamp and starting the cycle all over again.
The cachedAfter attribute represents a single date/time
after which ColdFusion will cache a query indefinitely. Until
the specified date/time passes, ColdFusion will continue to execute
this query against the database and return a new recordset. However,
once the specified date/time passes, ColdFusion will cache the recordset in memory and use the cache for all subsequent CFQUERY
calls.
You can find this setting in the ColdFusion Administrator in the Caching section. As you may have guessed by its label, you specify a maximum number of queries that ColdFusion can cache into memory. This setting prevents you from clogging ColdFusion’s memory space with an excessive number of cached queries.
There are a number of pros and cons associated with using this method of query caching.
cachedWithin parameter, there is no
easy way to clear the cached query. Clearing a query when you have
used this method requires that you execute the query with a time span
of zero (for example, createTimeSpan(0,0,0,0)). Additionally,
you cannot clear the cached query without executing it.cachedAfter attribute, there is no easy
way to clear the cached query. Clearing a query when you have used
this method requires you to execute the query while passing a date/time
that has already passed (such as 8/1/1975). Additionally, you cannot
clear the cached query without executing it.cachedWithin
or cachedAfter attributes, you cannot include cfqueryparam
tag calls. Since the cfqueryparam tag offers many useful
features for checking and enforcing data types, this is a big drawback.
This limitation does not, however, apply to the solution I present
on the following pages.