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.







	
    
        SELECT first_name,last_name,email,zip
        FROM member
        LIMIT 30
    
    
	
    


#message#

You should see something like this:

coldfusion 9 ehcache example

When you refresh the page, you should see the exact same thing, only with a different message:

coldfusion 9 ehcache example number 2

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:



	

Now if we put it all together, we’ve got:



	








	
    
        SELECT first_name,last_name,email,zip
        FROM member
        LIMIT 15
    
    
	
    


#message#

Just a couple of notes to end the post:

  1. Caching isn’t meant for everything, you should first figure out if the application would benefit from it in the first place.
  2. You can easily use to clear the specific cache on multiple servers, so don’t be afraid of caching dynamic pages throughout multiple servers.
  3. You’re not going to run out of caching memory, but always refer to the first note.
  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *