Over a million developers have joined DZone.

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

DZone 's Guide to

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

· Java Zone ·
Free Resource

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:

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:


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.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}