In ColdFusion 9, caching was done at the server level. All the cache properties like timeToLive, timeToIdle, were defined at the server-level in ehcache.xml present in C:\ColdFusion9\lib\ directory.
Wouldn’t it be useful to have caching at the application level? Each application will have different settings and configuration. It will be very useful if objects or templates of a particular application is maintained in the application-level cache rather than server-level cache. The objects can be cached for a definite period of time as required by the application. Maintaining these objects are also easier. This can also improve the performance and reliability of the application server greatly.
Therefore, in ColdFusion 10 we have provided support for application-level caching. This feature will allow developers to have application specific caching configuration by having ehcache.xml settings specified at application level. Developers will be able to specify path to ehcache.xml in Application.cfc. All the cache functions like cacheGet, cachePut etc. will use this Application specific cache configuration. Functions like cacheGetAllIds() will return all cache Ids for an application if Application configuration is applicable, else it will return all Ids defined at server level.
Using this application specific caching is extremely simple in ColdFusion. With a single line of code in Application.cfc which contains the path to the application specific ehcache.xml, application level caching can be achieved. Simple right? Newbies to Caching in ColdFusion can also get up to speed using application level caching. To make the concept of application specific caching more clear, I will illustrate a small example which puts a struct object into Application specific cache. The “timeToLive” is 5 seconds which is defined in ehcache.xml. So after 5 seconds the object will be removed from the cache. The directory structure of this example is:
Appsecific_cache
- Application.cfc
- Appspecific.cfm
- Ehcache.xml
Where Appspecific_cache is the folder which contains the respective cfm and cfc.
Application.cfc:
<cfcomponent>
<cfscript>
this.name = "appSpecificCacheTest";
//Specifying the path to ehcache.xml
this.cache.configfile = "ehcache.xml";
</cfscript>
</cfcomponent>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache
maxElementsInMemory="5"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="5"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="3600"
memoryStoreEvictionPolicy="LRU"
clearOnFlush="true"
/>
<cache name="app1cache"
maxElementsInMemory="5"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="5"
overflowToDisk="false"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="3600"
memoryStoreEvictionPolicy="LRU"
clearOnFlush="true"/>
</ehcache>
Appspecific.cfm:
<!--- remove object from Application Specific cache --->
<cfif ArrayLen(cacheGetAllIds()) gt 0>
<cfset cacheRemove(ArrayToList(cacheGetAllIds()))>
</cfif>
<!--- Creating new struct object --->
<cfset obj1 = structNew()>
<cfset obj1.name = "xyz">
<cfoutput>Starting to write to cache..</cfoutput>
<cfset cachePut("obj1",obj1)>
<br/>
<cfoutput>Done!!</cfoutput>
<cfoutput>Trying to fetch cached item...</cfoutput>
<cfset obj = cacheGet("obj1")>
<br/>
<cfdump var="#obj#">
<!--- Giving a sleep of 15 seconds which is greater than timeToLive --->
<cfscript>
sleep(15000);
</cfscript>
<cfoutput>Trying to fetch cached item after 15 seconds... <br/></cfoutput>
<cfset obj = cacheGet("obj1")>
<cfdump var="#obj#">
After executing Appspecific.cfm, we can see that initially the object is present in the cache. After 5 seconds the object is removed from cache because timeToLive is 5 seconds.
This is just a very simple example to demonstrate application specific caching. The user can change the settings of the cache by modifying the ehcache.xml according to their convenience. Many more such operations can be performed.
More to caching in my next post..Till then enjoy Application-level caching. :)