Sunday, February 19, 2012

Caching in ColdFusion - Application-level caching


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









Wednesday, February 8, 2012

Solving Space issues in Solaris

Most of us have encountered couple of space issues with Solaris. But sometimes it so happens that there will be space available in the machine but still one cannot install the required software. The below message is shown:

"There is not enough space to install the software. It requires 1,000,223 bytes of space but your machine has just 308,777 bytes free.Please free 691,446 bytes of memory ".

After seeing this message, the most obvious thing which all of us would do is find how much space is available by  using df -eh command. Surprisingly there will be a lot of space on the machine but still the same message is thrown. Thoughts and questions arise as to why?what?how? The amateurs will find it extremely difficult to proceed further.

In the above situation the solution is very simple. This simply happens because the "tmp" directory under root  has very less space. This "tmp" directory stores temporary files during installation by default. So one should manually set the temporary directory to any directory which has space. This is done by the following command:

#export IATEMPDIR=<directory which has space>

For example, If the "tmp" directory under /opt has sufficient space, then the command should be:

#export IATEMPDIR=/opt/tmp/

This can also be:
#IATEMPDIR=/opt/tmp/
#export IATEMPDIR


By doing the above, the temporary directory where the install files are kept is changed to a new directory which has sufficient space. The software will install properly with no issues now.
Hope this post will be helpful for folks who are stuck up with this issue.