Thursday, September 22, 2011

Caching in ColdFusion-Brief Overview

Well... Am sure most of us will be aware of what a Cache does.It is nothing but a component that temporarily stores data so that it can be referenced when future requests to the same data are made at a faster rate. Thus, it saves the time required to access the data from the disk or any other location improving the efficiency. The various advantages of caching are:

1. Enables accessing of the data at a faster rate.
2. Improves performance
3. In case of Web based applications, caching reduces the load on the Web server or Application server.
4. Increases reliability.(If the main server goes down, then data can be retrieved from the cache or vice-versa)

Adobe ColdFusion is an application server which enables developers to build, deploy and maintain large Internet based applications which store large amount of data. Since large amount of data transactions will be carried out, caching is a very important requirement. In case of ColdFusion a Web cache sits in between one ore more Web servers and one or more clients. This Web cache stores the data and can be accessed when the request for that data is made. This avoids the call to the Web Server to fetch the data thus reducing Latency. As the data is reused it reduces the representations used by the client which reduces the amount of bandwidth used by the client.  This lowers Network Traffic. Since the load is borne both by the Web cache and the Web Server's Database or the App server's database, the memory is efficiently managed which reduces the Garbage collection and the load is efficiently distributed. This makes the data more reliable since data is present in both the places and if server goes down data can still be retrieved from the cache.


We all know that ColdFusion makes use of ehcahe as the cache provider for caching data. By default, the ehcache is stored within the CF address space. The different Caching architectures which are used are:

  1. In-Process Architecture : Operates in the same JVM as the application server. It has limited scalability for 32-bit systems as memory constraint must be considered. The data access is faster in this architecture as data/object serialization is not required.                                                                                             
  2. Out-of-Process Architecture : Operates in its own process outside the application server's JVM. It is highly scalable on both 32-bit and 64-bit platforms. It is generally slower than In-Process architecture as data/objects must be serialized/deserialized. 

The different types of Cache regions present in ColdFusion by default are:
  1.     Object Cache: It is the most flexible way to cache data. it can cache anything put in a ColdFusion variable.It has granular control over cache functions.It has access to the various cache item meta data such as cache_hitcount,cache_misscount,hitcount,idletime etc. It uses ehcache as cache provider.
  2.     Template Cache :  It can be used to cache a whole page by placing a <cfcache> tag at the top for the page. It can cache page fragments by placing the <cfcache> tag in between pages which have to be cached. It is useful when part of the page should be dynamic. It uses ehcache as cache provider
ColdFusion had caching in the <cfquery> tags in the previous versions. Query cache does not use ehcache as cache provider. In version 9, there were many enhancements added. Some of them were:
  1. Disk based and Memory based Caching
  2. Cfcache to use ehcache as default cache provider. This ehcache.xml is present under     <coldfusion_home>/lib directory
  3. Caching Page Fragements
  4. Introduced new functions such as
    Refer to 7fe1.html for detailed explanation.
 5. New attributes are added to the <cfcache> tag . Refer for detailed explanation.