Query caching in Railo and CFMX

Caching queries is something that one should use since it can dramatically speed up the delivery of a page.

Anyhow, there are and were some differences between Railo and CFMX. One was, that in Railo if you wanted to flush the querycache, you only had the way of using the tag: <cfobjectcache action="clear">. This would then purge all the queries from the query cache. But what if you wanted to flush only one single query from the cache?
CFMX (and now Railo) offers the way of caching a query like follows:


<cfquery name="getInformation" datasource="railo" cachedwithin="#CreateTimeSpan(0,0,0,0)#">
Select * from test_first
</cfquery>
This then, according to the adobe documentation and as you can read here, would flush a certain query from the query cache. This works in Railo, but in CFMX you have to be aware of the following. Let's assume you have the following code:

<cfquery name="getInformation" datasource="railo" cachedwithin="#CreateTimeSpan(0,0,0,0)#">
Select * from test_first
</cfquery>
<cfdump var="#getInformation#">
<cfquery name="updateInformation" datasource="railo">
Update test_first set name = 'b+c+d#GetTickCount()#' where p_id = 2
</cfquery>
<!--- if I would select and dump the query again, no change would be reflected, so according to the Adobe Documentation the following query should flush the query from the cache --->
<cfquery name="getInformation" datasource="railo" cachedwithin="#CreateTimeSpan(0,0,0,0)#">
Select * from test_first
</cfquery>
<!--- Now this displayes the changed query: correct --->
<cfdump var="#getInformation#">
<!--- This is just to have a certain delay in the update --->
<cfset sleep(1000)>
<!--- Assuming the documentation is right, the query should not be in the cache anymore, so I am updating it --->
<cfquery name="updateInformation" datasource="railo">
Update test_first set name = 'susi#GetTickCount()#' where p_id = 2
</cfquery>
<cfquery name="getInformation" datasource="railo" cachedwithin="#CreateTimeSpan(0,0,1,0)#">
Select * from test_first
</cfquery>
<!--- Very interesting is, that this code does not reflect the last change I made, instead I get the query that has been stored into the cache a couple of lines above when I used the cachedwithin="#CreateTimeSpan(0,0,0,0)#" setting --->
<cfdump var="#getInformation#">
So I am assuming that the setting cachedwithin="#CreateTimeSpan(0,0,0,0)#" only refreshes the query in the querycache. It does NOT remove it. In Railo (2.0.1.002 or later) the query will be removed from the query cache.
In addition every query CFMX gets from the cache is copied to the variable you use for it. In Railo you will only get a pointer pointing to the cached query. It will only be copied as soon as it is changed with QuerySetCell() or similar functions. This of course is much faster in 90% of the cases than copying it every time. This is why Queries in CFMX even though cached can consume some time. In Railo they are almost always executed with 0ms. You can read an earlier post about querycaching.

In Railo 2.1 we will enhance the functionality of the <cfobjectcache> tag. Sometimes, when you update a table and want all queries relying on this table to be flushed from the cache. So we will add the attribute filter to the tag. You can pass a regular expression to it like follows:

<cfobjectcache action="clear" filter="test_([0-9]+)"
This will flush all queries matching the regular expression from the query cache. So you are in perfect control about which queries should be flushed.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner