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.
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.