DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • What to Pay Attention to as Automation Upends the Developer Experience
  • A Data-Driven Approach to Application Modernization
  • How Web3 Is Driving Social and Financial Empowerment
  • Microservices With Apache Camel and Quarkus (Part 2)

Trending

  • What to Pay Attention to as Automation Upends the Developer Experience
  • A Data-Driven Approach to Application Modernization
  • How Web3 Is Driving Social and Financial Empowerment
  • Microservices With Apache Camel and Quarkus (Part 2)
  1. DZone
  2. Data Engineering
  3. Data
  4. Secondary Level Caching in ColdFusion ORM

Secondary Level Caching in ColdFusion ORM

John Whish user avatar by
John Whish
·
Jul. 25, 11 · News
Like (0)
Save
Tweet
Share
7.77K Views

Join the DZone community and get the full member experience.

Join For Free

I've been doing some performance improvements to a site by making use of the ORM Secondary Level Caching that is built into ColdFusion 9. Interacting with databases is a common bottleneck in applications so you want to reduce the amount of queries you need to run. ColdFusion has had the ability to cache queries for a long time by simply adding the cachedWithin attribute to cfquery. When you're using ORM, then there are similar in-built caching capabilites.

Firstly you need to enable the secondary level cache in your Application.cfc

this.ormSettings.secondaryCacheEnabled = true;

Once you've done that they you need to add the cacheuse attribute to the persistent CFCs (your entities) that you want to be cached. There are several values you could use, but the one I'm using is transactional.

component persistent="true" table="contents" cacheuse="transactional" cachename="content"
{
  property name="id" column="content_id" generator="native";
}

The cachename attribute is optional, by default it will use the name of your entity.

Now when you use EntityLoadByPk to load a Content entity, then ColdFusion will automatically look in the cache first. If it finds it, it will use it, if it doesn't then it will query the database. As I've set the cacheuse value to "transactional" in my CFC, when I update (and persist) the entity, then the cache is updated for you so you don't need to worry about stale entries in the cache.

One of the great things about the ORM caching is the fine grained control is gives. One of the worst things about the ORM caching is the fine grained control is gives you! What do I mean? Well, consider the following:

a = EntityLoadByPK( "Content", 1 );
b = EntityLoad( "Content", 1, true );
c = EntityLoad( "Content", {id=1}, true );
d = EntityLoad( "Content", {id=1} ); // returns an array
e = ORMExecuteQuery( "from Content where id = :id", {id=1}, true );
f = ORMExecuteQuery( "from Content where id = :id", {id=1} ); // returns an array

They all get the content entity with an id of 1, but which ones would you expect to be pulled from the cache? The answer is that ORMExecuteQuery will not use the cache. This is not surprising as you could be using custom HQL but it does mean we have to do some extra work if we want to cache results from HQL:

e = ORMExecuteQuery( 
    "from Content where id = :id", 
    {id=1}, 
    true,
    {cacheable=true, cachename='content', timeout=60}
);

The above will cache the result with a key name of "content" for 60 seconds. If you update the entity within those 60 seconds, then you won't get the updated entity. This is similar to how it works with cached queries, but you need to be aware of it.

If you prefer to write your HQL in cfquery blocks (ColdFusion 9.0.1) then you'll need to use the new ormoptions attribute:

<cfset options = {cacheable=true, cachename='content', timeout=60}>
<cfquery name="q" dbtype="hql" ormoptions="#options#">
    from Content where id = <cfqueryparam value="1">
</cfquery>

<cfset e = q[1]>

 

Cache (computing) Database

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • What to Pay Attention to as Automation Upends the Developer Experience
  • A Data-Driven Approach to Application Modernization
  • How Web3 Is Driving Social and Financial Empowerment
  • Microservices With Apache Camel and Quarkus (Part 2)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: