Jul
24
ColdFusion 9/10 Query Caching with Ehcache – Part 1
I’ve been spending a lot of time trying to figure out where I can improve the overall speed of web pages and websites in general and it’s come to my attention, that a lot of ColdFusion developers have not worked with some of the new caching capabilities available to us. I believe most of the reason is that their company is still on ColdFusion 8 and hasn’t found a reason or has the capital to upgrade to at least CF9, let alone CF10.
Whether you’re preparing yourself for when you’re working with at least CF9 or maybe you need a quick refresher on some syntax, I’m going to provide a small example that can be used in a number of applications.
For the first part of these blog posts, we’re going to take a look at how to store a query object in Ehcache with ColdFusion 9.
There are three functions in particular you should remember; cacheGet(), cachePut() and cacheRemove(). They’re used exactly how they’re named and their purpose is for accessing the Ehcache layer.
<!--- check for the query object in the cache---> <cfset getAllMembers = cacheGet("qry-getAllMembers") /> <!--- we're going to set a message just to see what runs ---> <cfset message = "Data came from the cache" /> <!--- 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)) /> <cfset message = "Data came from the query" /> </cfif> <!--- output the message just to confirm you're retrieveing the data from the correct source---> <p><cfoutput>#message#</cfoutput></p> <!--- dump the query object ---> <cfdump var="#getAllMembers#" />
You should see something like this:
When you refresh the page, you should see the exact same thing, only with a different message:
Now there’s going to be times when you need to clear the cache, and that can be easily done by calling cacheRemove(). In this example, I’m just checking for a url variable and and then calling the function so we can requery the data and then recache the new query object.
Let’s take a look at a small piece of code we can add to refresh the cache object:
<!--- check for the url.resetcache variable to clear the current cache object ---> <cfif StructKeyExists(url,"resetcache")> <cfset cacheRemove("qry-getAllMembers") /> </cfif>
Now if we put it all together, we’ve got:
<!--- check for the url.resetcache variable to clear the current cache object ---> <cfif StructKeyExists(url,"resetcache")> <cfset cacheRemove("qry-getAllMembers") /> </cfif> <!--- check for the query object in the cache---> <cfset getAllMembers = cacheGet("qry-getAllMembers") /> <!--- we're going to set a message just to see what runs ---> <cfset message = "Data came from the cache" /> <!--- 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 15 </cfquery> <!--- insert the query into the cache ---> <cfset cachePut("qry-getAllMembers",getAllMembers,CreateTimeSpan(0,1,0,0)) /> <cfset message = "Data came from the query" /> </cfif> <!--- output the message just to confirm you're retrieveing the data from the correct source---> <p><cfoutput>#message#</cfoutput></p> <!--- dump the query object ---> <cfdump var="#getAllMembers#" />
Just a couple of notes to end the post:
- Caching isn’t meant for everything, you should first figure out if the application would benefit from it in the first place.
- You can easily use
to clear the specific cache on multiple servers, so don’t be afraid of caching dynamic pages throughout multiple servers. - You’re not going to run out of caching memory, but always refer to the first note.
- Fact: The faster the website, the greater the conversion rate.
I’m not going to throw up a demo because there’s really nothing to look at, but I’ll provide the code with the database dump of a large sample table for you to do some testing with.