ColdFusion 10 Query Caching with Ehcache – Part 2

A quick follow up to the part 1 of caching queries in Ehcache with ColdFusion.

In ColdFusion 9, we were forced to work the long way of figuring out if a query object was in the cache and if it wasn’t then to use cachePut() and insert it. With ColdFusion 10, we’ve got some improved caching features that makes the default cache be the Ehcache layer.

Instead of writing:

<!--- check for the query object in the cache--->

<cfset getAllMembers = cacheGet("qry-getAllMembers") />

<!--- check to see if the cache object has any data --->

<cfif isNull(getAllMembers)>

<!--- query for some data --->

<cfquery name="getAllMembers" datasource="local_sample">

SELECT first_name,last_name,email,zip

FROM member

LIMIT 30

</cfquery>

<!--- insert the query into the cache --->

<cfset cachePut("qry-getAllMembers",getAllMembers,CreateTimeSpan(0,1,0,0)) />

</cfif>

<!--- dump the query object --->

<cfdump var="#getAllMembers#" />

All we’re going to need to write is the query, with one new attribute: cacheId.

<!--- query for some data and cache the query --->

<cfquery name="getAllMembers" datasource="local_sample" cacheid="qry-getAllMembers" cachedwithin="#CreateTimeSpan(0,0,12,0)#">

SELECT first_name,last_name,email,zip

FROM member

LIMIT 30

</cfquery>

<cfdump var="#getAllMembers#" />

When you’re testing this new feature out, what you should do is confirm that the cacheId exists in the cache and that everything is working accordingly. We’re going to retrieve a list of cache objects in the Ehcache by calling cacheGetAllIds(). This function allows you to enter the name of the cacheRegion and you can also define your cacheRegion as another attribute on the tag. By default, the cacheRegion is set to "query", so all you’ll need to do is update the function to be cacheGetAllIds(“query”). When we take a look at the final code, we’re going to end up with what you see below. Again, you don’t have to define the cacheRegion attribute on the , but I’m just demonstrating.

<!--- retrieve all of the cache objects for the query cache region --->

<cfdump var="#cacheGetAllIds("query")#" />

<!--- query for some data and cache the query --->

<cfquery name="getAllMembers" datasource="local_sample" cacheid="qry-getAllMembers" cachedwithin="#CreateTimeSpan(0,0,12,0)#" cacheregion="query">

SELECT first_name,last_name,email,zip

FROM member

LIMIT 30

</cfquery>

<cfdump var="#getAllMembers#" />

You should see something like this:

coldfusion 10 ehcache example

Again, there really isn’t much to demo, but I’ve provided the code and a large sample table for you to do some testing with on your own.

As a final note, if you do wish to not use Ehcache as your default cache layer and you wish to use the previous default caching layer, you can do so by checking a box in the ColdFusion administrator. I’ve added a screenshot below to help you find the option.

disabling coldfusion 10 ehcache as default cache

1 comment on this post.
  1. Renu:

    Displaying #start# to #endc# of #records.recordcount# records

    < Prev  

    #NumberFormat(pc,’00’)#

    #NumberFormat(pc,’00’)#

      Next >

Leave a comment