Jun 6 2010

Caching In ColdFusion - Native Caching

Posted by Mike Brunt at 2:47 AM
15 comments
- Categories: CloudComputing | Caching | ColdFusion | JRun-J2EE

Used effectively and managed well, caching is a very effective tool in building highly scalable web applications, for example Facebook and Twitter both use large distributed caching mechanisms to ensure acceptable performance with immense amounts of data. What is a cache in computing terms? Let's get it from "the horses mouth"; the EHCache users guide. "In computer science terms, a cache is a collection of temporary data which either duplicates data located elsewhere or is the result of a computation. Once in the cache, the data can be repeatedly accessed inexpensively".  EHCache is a Java based mechanism which ships natively with ColdFusion 9 and which we will get into in more detail on, later.  The purpose of this blog post is to list the different kinds of caching available to us in ColdFusion.  Here they are and we will indicate whether memory or disk based on each.  One very important point to bear in mind in this regard, if the caching is memory based the cache will reside within the same JVM memory spaces as everything else.  That space is very finite in 32-bit installs of ColdFusion.  Before getting into detail, I recommend you read a previous blog piece on something I called "Data Distance"  this gives an overview of data manipulation and caching is a major piece in manipulating data; effectively.

Another important need, in effective caching, is the ability to manage the cache, to see what is in there and to assess the success hit rate and to control cached content updates. We comment on this below in the "Management" sections.

Caching in ColdFusion has been around through many versions and in some cases only lasts for the duration of a single request unless coded to persist, an example of caching only for the duration of the request would be a request using "Query of Query", a very handy ColdFusion construct . In this section we will detail persistent caching in ColdFusion.  Typically caching is used in QA to gauge performance improvements via load-testing and in Production to deliver those performance gains to end users.  Caching should be off in development as of course we typically want to see the results of code changes immediately.

SHARED MEMORY SCOPES (MEMORY): This is often not seen as caching, per se, but it definitely is, if fact Ray Camden created a construct to utilize shared memory scopes for caching, it was called "ScopeCache". The shared memory scopes can contain simple or complex data types and are:

 

  • Server - Available to all applications and users on a single ColdFusion server.
  • Application - Available to all users in a single ColdFusion application as defined by the THIS.name = "foo"; 
  • Session - Available to a single user in a single application.

 

Management - Each scope can have a timeout set (THIS.sessionTimeout = createTimeSpan(0,0,20,0);) and the items in that scope will expire on reaching that time-out.  Alternatively, they can be overwritten by simply recreating them.  Items cached in shared memory scopes can be difficult to manage.  One last caveat here, if using shared memory scopes use the  tag when creating the cache to make sure that "race conditions" with unpredictable data do not occur.  The contents of single variables, nested variables or complete scopes can be viewed using the tag.

QUERY CACHING (MEMORY): Used effectively query caching can be a great performance enhancer, reducing multiple trips to the database by storing the resultsets in memory.

Management - Similarly to shared memory scopes, timeouts can be set using "CachedWithin" CachedWithin="#CreateTimeSpan(0,1,0,0) which will flush the cached content after that time.  Items stored within a cached query can be difficult to manage.  Note in ColdFusion Server Monitor, it is possible to get a snapshot of cached queries via the snapshot dumped via the Alter Configuration in ColdFusion administrator > Server Monitor, this can be useful in assessing the effectivity of cache hits.

CONTENT CACHING - CFCACHE (MEMORY): Cache copies of full page content on the server or client side. CFCACHE has simple URL parameter detection to assist in serving or not serving cached content.  There is also a timeout which can be set - timeout = "#DateAdd(datepart, number, date)#"

Management - Once again management of the cached content via CFCACHE is not easy and flushing what is there is achieved by overwriting existing content or when the timeout setting is reached.  There is a small amount of intelligence that can be passed via URL strings.

TEMPLATE-TRUSTED CACHE (MEMORY): Of all the pre ColdFusion 9 caching mechanisms my most preferred one is Trusted Cache.  Trusted Cache takes the parsed ColdFusion code in a template (not the view content) and caches it the first time it runs.  I have seen it deliver anything from 25% to 40% improvement in overall performance and it is predictable.

Management - Trusted Cache is enabled via the ColdFusion Administrator and there is a control to flush the cache there, so it is simple to control.  Any time new code is added or existing code changed the cache should be flushed.  Another important point in ensuring the effective use of Trusted Cache is to make sure the maximum number of cached templates is set to at least the total of all .cfc and .cfm templates in the ColdFusion application.  This is set in ColdFusion Administrator.

CACHE TEMPLATE IN REQUEST (MEMORY): ColdFusion 9: Caches a template for the duration of the Request. useful where multiple calls to same template in same request.  I often see multiple calls to the same template in applications which use cfc based frameworks.

Management - Effectively there is none as the items are only cached for the duration of the Request.

COMPONENT PATH CACHE (MEMORY): ColdFusion 9: This caches the path to CFC’s (when paths are deeply nested this can cause performance problems, so ideally deep nesting should be avoided, when coding applications).  

Management - As with Trusted Cache, this can be flushed via the ColdFusion Administrator.

SAVE CLASS FILES (DISK): When ColdFusion code is parsed it is compiled into Java classes which are in memory at that point.  Having this checked in the ColdFusion Administrator means that those classes will be saved to disk.  This is good both for ColdFusion performance between restarts and also for troubleshooting what code actually ran as the class file name is retained in the logs, should errors occur.

Management - Simply enabled via the ColdFusion Administrator.

CACHE WEB SERVER PATHS: In reality this should never be used unless there is a single web site on the web server in use which is highly unlikely.

Management - make sure this is turned off in the ColdFusion Administrator.

EHCACHE (Memory-Disk): ColdFusion 9: Native to CF9 a powerful Java based caching mechanism which can be local (heap) and is replicable-distributable. We are very excited about the possibilities afforded by EHCache as it brings the sorts of distributed caching mechanisms available to applications such as FaceBook and Twitter to ColdFusion.  We are just beginning evaluation and testing of EHCache and will publish our findings going forward.

Management - EHCache has programmable and sophisticated API's for assessing success rates and managing cache content; we will be blogging more on this in future articles on EHCache, specifically.

ORM HIBERNATE (Memory-Disk): - ColdFusion 9: Native to CF9, Object Relational Mapping for database abstraction, which uses EHCache as its caching mechanism.

Management - Managed at the code level.

VIRTUAL FILE SYSTEM (Memory): ColdFusion 9: In memory file storage which can be useful when handling files, temporarily.

Management - Managed at the code level.

In this blog piece, we have detailed all caching mechanisms available in ColdFusion 9 (as stated) and in previous versions of ColdFusion where not stated as ColdFusion 9.  Our next blog piece on caching will go into detail on using EHCache in Production applications.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

CFWHISPERER

CFWHISPERER wrote on 08/12/10 12:03 AM

We are currently working with the creators of EHCACHE and Terracotta to bring something exciting to CF.
hr services

hr services wrote on 02/12/11 9:32 AM

The article was really wonderful and I enjoyed reading it. I have had a problem recently. I have deleted the cold fusion pages off the server and after a few months later, it appears there again. I just want to make sure that it doesn’t reappear again. I updated it and it was gone after a few days it was there again. Any idea what might be causing it? I am really looking forward to getting a feed back to solve the problem.
shiyan

shiyan wrote on 02/14/11 12:19 AM

.[url=http://www.pulati.cc/]???[/url] pilates.
torrent

torrent wrote on 02/24/11 8:00 AM

Nice post. I've been using the query cacheing for years but I've never really thought about it in this much detail. In the coldfusion monitor I'm not seeing any templates in cached queries list. The graph in the query cache status is working. Any suggestions?
nike air max 90

nike air max 90 wrote on 05/24/11 9:10 PM

Definitely agree with what you stated. Your explanation was certainly the easiest to understand. I tell you, I usually get irked when folks discuss issues that they plainly do not know about. You managed to hit the nail right on the head and explained out everything without complication. Maybe, people can take a signal. Will likely be back to get more. Thanks
acupuncture pain fl

acupuncture pain fl wrote on 06/02/11 12:29 AM

Knowing how acupuncture works, after that you can use it effectively as a nonstandard treatment for pain. An <a href="http://www.xinglinhealing.com/">acupuncture pain fl</a> will insert very thin needles into specific points in your body. Acupuncture was initially used in China nearly two millennia ago. It first started to get attention in Usa in 1971, because it was effective in relieving postoperative pain. In this article, we'll discuss the various theories regarding precisely how acupuncture works.

Chinese theories as to how acupuncture heals the human body differ greatly from those discussed in the West. Based on chinese medicine beliefs, there are two opposing forces anyway, yin and yang. These also occur in the body. Yin is very passive, while yang is very active.
Locksmith Paterson NJ

Locksmith Paterson NJ wrote on 06/24/11 12:31 AM

Top class help; I am going to absolutely keep returning to your blog post for more content.
<a href="http://www.PatersonNJLocksmithBest.Com">Locksmith Paterson NJ</a>
Custom flag

Custom flag wrote on 07/12/11 3:03 AM

ohh man this is awesome post.i think they should usebanner to get more flow in look.
you know this is great working by the writer.The writer has done a great job in speniding his time in
research about this article. can i subscribe all his posts ?
I am really happy to read this. i was searching this from last two months and atlast i got it. hurrah..!!
discount chanel handbag

discount chanel handbag wrote on 07/21/11 7:32 PM

A very good post . Thanks for sharing it. welcome to our website and we will give you a big discount for all the products.
cheap handbag

cheap handbag wrote on 08/22/11 7:54 PM

The concept of your article is pretty unique which is a good element in driving more individuals to read your site.I even told my friends to check out your blog and in fact your blog is already bookmarked on my computer. I am looking forward to see your upcoming post. Keep it up!
hermes

hermes wrote on 09/07/11 2:42 AM

Thanks a lot for sharing this. I have never thought that surfing online can be so much beneficial and entertained in a good shape. I feel really happy and grateful for providing me with such priceless sound track. All are good here simply best. I want such article again and again.
cuu du lieu

cuu du lieu wrote on 11/07/11 2:14 AM

The Government needs to come clean on what its Paid Parental Leave Scheme really means for working families, starting with its name.
superdry sale

superdry sale wrote on 11/18/11 6:07 PM

Every superdry sale fans convinced superdry will be next huge designer. The colors of superdry uk are just amazing because they are universally flattering! It is a great alternative for people who are tired of buying out the entire superdry outlet line and are finished shopping at Our Superdry uk sale store.
qjkider

qjkider wrote on 11/24/11 4:14 PM

Daily meticulously shaping a hair, will make people look extra spirit [url=http://www.bestlisseurghdfr.info/][b]ghd lisseur[/b][/url] Today we introduce some friends of female beauty does not hurt the hair shaping straight hair, [url=http://www.bestlisseurghdfr.info/][b]styler ghd [/b][/url]the product uses a high temperature ceramic plates, [url=http://www.bestlisseurghdfr.info/][b]lisser ghd[/b][/url] hair can minimize the damage will be. Straight hair looks elegant design [url=http://www.bestlisseurghdfr.info/][b]ghd hair straighteners[/b][/url] Body with a high temperature ceramic plates smooth and delicate texture to prevent their hair, [url=http://www.bestlisseurghdfr.info/][b]ghd france[/b][/url] to protect the scalp healthy.

Write your comment



(it will not be displayed)



Leave this field empty: