Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

The one where I try ColdFusion 9 ORM secondary cache and decide its awesome

February 7, 2011

I've pretty much overlooked the secondary cache for ORM and, having now played with it, I've realized what a big feature and performance enhancer this is.

In short with secondary cache on ColdFusion can get data from ehCache instead of the database. Set up ColdFusion 9.0.1 and you can share entity data across both ColdFusion instances and servers!

Let backtrack and take a look at a basic set up:

Application.cfc component { this.datasource = "cacheFun"; this.ormEnabled = true; this.ormSettings = {secondaryCacheEnabled=true, logsql=true}; }

To turn on secondary cache set secondaryCacheEnabled to true (the default is false). In this example I have also turned logsql to true*. Now lets look at our persistent cfc:

User.cfc component persistent="true" cachename="userCache" cacheuse="transactional" { property name="id" column="id" ormtype="int" fieldtype="id" generated="always" generator="native"; property name="firstname" ormtype="string"; property name="lastname" ormtype="string"; property name="version" fieldtype="version" datatype="int" ; function getLastName() { return uCase (variables.lastname); } }

That really is it. The two cache attributes tell ColdFusion to use secondary cache for this data. From now on a call like:
user = entityLoad("User", {id=1}, true);
will use the secondary cache when possible.

To help work out what secondary cache is doing I did two things in User.cfc:

  1. added a version field which would definitively show when Hibernate had updated the record (another very cool underused feature)
  2. added my own getter to verify that secondary cache returns data via the cfc and not just flat

Now there are four types of cacheuse, here are the differences:

I'm going to look more into secondary cache and hopefully come up with real examples but for now I know two things: 1) its very powerful, 2) its very easy to use.

* If ColdFusion is started from the Builder server panel the console tab will display all the SQL Hibernate produces. I used it to see when Hibernate went to cache and when the database.


2 Comments

I only recently found out about this myself too. It's kinda scary how well it works. I keep thinking, "It can't be this easy!".

By: Raymond Camden 02/07/2011 10:51 PM
"It can't be this easy" -- That should be the catchphrase for ColdFusion (or jQuery!)

By: Sam Farmer 02/08/2011 10:02 PM
Some comments have been lost over the years due to moving hosts.

More


More blog entries that I have written.

Code coloring by PRISM.