Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened. But I have become a passionate and pretty good web developer. I code mostly in ColdFusion and jQuery. Sam is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

The one where I try ColdFusion 9 ORM secondary cache and decide it's awesome

04.19.2011
| 6524 views |
  • submit to reddit

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:

  • transactional cache is updated on the update statement and the next request of the data will get it straight from cache and not run a select
  • nonstrict-read-write Cache is updated on the first select after data is updated or inserted
  • read-write I could not get this to work with caching (docs say its the worst performing anyway)
  • read-only Only works with data that will not be updated. Produces an error if you attempt to update or insert records. (Marc Esher wrote a long, funny blog entry about using secondary cache to cache for a read-only view)

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.




References
Published at DZone with permission of Sam Farmer, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: