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









44 comments:

  1. A ColdFusion application has access to multiple persistent scopes:The Application scope contains variables available to all users of an application. It is typically used to cache the data that everyone needs.STC Technologies|STC Technologies

    ReplyDelete
  2. I appreciate from this post and its seems looking so informative ad networks for good target for Indian's. Thanks for sharing with us..
    http://packersmoversbangalore.in/
    http://packersmoversbangalore.in/packers-and-movers-hennagara-bangalore

    ReplyDelete
  3. Thanks for sharing useful information for us.I really enjoyed reading your blog, you have lots of great content.Please visit here:
    http://packersmoversahmedabad.co.in/
    Ahmedabad Packers And Movers is recognized as a business manager providing wide-ranging and differentiate service appearance as well as Relocation Shifting, Logistics and Transportation, Facilities managing, strategy & Designing services.
    thanks a lot for all your efforts...
    Packers And Movers Ahmedabad

    ReplyDelete
    Replies
    1. Nicely explained. Here you described the well written article from your in-depth knowledge. Truly impressive and nice information

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

      Delete
  4. an awesome blog thanks a lot for giving me this great opportunity to write on this.please visit our website I assure you that it will be very useful for you-http://packersmoverschennai.in/
    http://packersmoverschennai.in/packers-and-movers-park-town-chennai
    http://packersmoverschennai.in/packers-and-movers-kotturpuram-chennai

    ReplyDelete
  5. Packers and Movers Delhi
    Packers And Movers Delhi are not here to simply add the awesome substance to our webpage instead just to peruse by, however real dedicated online office is giving you their best for your benefit. In city itself, individuals once are at some point arbitrary guests to this site as you might be are right now; they turn out to be amicable regular clients of our own and even do prescribe us to others for we give the solid and reliable moving administrations.
    http://packers-and-movers-delhi.in/packers-and-movers-secretariat-north-delhi
    http://blog.packers-and-movers-delhi.in/

    ReplyDelete
  6. Packers and Movers Kolkata
    Packers and Movers Kolkata, Local Shifting Relocation and Top Movers Packers Kolkata. Packers and Movers Kolkata Household @ www.KolkataPackersMovers.in
    http://kolkatapackersmovers.in/packers-and-movers-bhawanipore-kolkata
    http://blog.kolkatapackersmovers.in/

    ReplyDelete
  7. Additional will not deal with the organization which is unwilling to protect virtually any information as well as solution a personal nicely. Packers and Movers Hyderabad
    Packers and Movers Pune

    ReplyDelete
  8. We furnish you with the names of the nearby merchants of distinctive urban areas for you to pick the best PACKERS AND MOVERS IN BANGALORE know the estimation of your money and along these lines MOVERS AND PACKERS BANGALORE give you the alternatives of the sensible, sensible and useful organization suppliers so you can go for them as per your needs, prerequisites and their deeds. You can now successfully consider the cost of your development through the diverse quotes gave by the clearing associations recorded with us.
    Packers And Movers Bangalore

    ReplyDelete
  9. really a vry nice blog i really appreciate all your efforts ,thank you so mch for sharing this valuable information with all of us.
    Working with us will reliably come to fruition into encompass. Numbering us with your development is practically as "What finish off an officially good thing". These are not our words these are the reviews composed by Packers Movers Hyderabad customer to our site.
    http://packersmovershyderabadcity.in/

    ReplyDelete
  10. I really enjoyed reading your blog, you have lots of great content.Please visit here:
    http://jaipurpackersandmovers.in/
    http://jaipurpackersandmovers.in/packers-and-movers-alwar
    http://jaipurpackersandmovers.in/packers-and-movers-ajmer
    We are expert in Handling, Movers And Packers Jaipur and Packing & Shifting of Household Goods, Office goods, Commercial Equipment and Car, Logistics and Transportation.
    Packers And Movers Jaipur

    ReplyDelete
  11. I really enjoyed reading your blog, you have lots of great content.Please visit here:http://packersmoverspune.org/
    http://packersmoverspune.org/packers-and-movers-pune-to-bangalore
    http://packersmoverspune.org/packers-and-movers-dalaj-pune
    Packers And Movers Punebased company provided that Movers And Packers Pune Services for Office, Home, Local or domestic and commercial purposes.

    ReplyDelete
  12. I like your website very much thanks for your share.all the imformation are useful for me,I will come again.Please visit here: http://bhubaneswarpackersmovers.in/
    Packers And Movers Bhubaneswar
    http://bhubaneswarpackersmovers.in/packers-and-movers-bhubaneswar-to-chandigarh

    ReplyDelete
  13. Thanks for sharing useful information for us.I really enjoyed reading your blog, you have lots of great content.Please visit here: http://packers-and-movers-gurgaon.in/
    Packers And Movers Gurgaon
    http://packers-and-movers-gurgaon.in/packers-and-movers-gurgaon-to-chennai

    ReplyDelete

  14. Thanks for a quick review. I will definitely not spend time evaluating their opensource product.

    Packers and Movers Kalpakkam 9380223600 Chennai

    ReplyDelete
  15. Packers And Movers Chandigarh i must say you had done a tremendous job,I appreciate all your efforts.Thanks alot for your writings......Waiting for a new 1..
    http://chandigarhpackersandmovers.in/packers-and-movers-mansa

    ReplyDelete
  16. Packers And Movers Mumbai Thanks for sharing useful information for us.I really enjoyed reading your blog, you have lots of great content.
    http://packersmoversmumbaicity.in/packers-and-movers-nagpur-maharashtra

    ReplyDelete
  17. really a vry nice blog i really appreciate all your efforts ,thank you so mch for sharing this valuable information with all of us. Please visit here: Packers And Movers Patna
    http://packersmoverspatna.in/packers-and-movers-bhatgaon-patna

    ReplyDelete
  18. Search for the best colleges in India for your higher education at http://enrichexpo.com. Get free career guidance and expert’s advice from the top career experts of globe avail attractive scholarship!!!

    ReplyDelete
  19. Thanks for sharing useful information for us.I really enjoyed reading your blog, you have lots of great content.Packers And Movers Bhopal
    Packers And Movers khandwa
    Packers And Movers Khargone

    ReplyDelete
  20. I came across and saw this post which seems to be a nice one.
    Life Experience Degrees

    ReplyDelete
  21. Packers and Movers in Mumbai
    Packers and Movers in Pune
    We are providing best shifting services in India Office shifting Relocating is made easy with the help of packing and moving firms. They work satisfactory in all the approaches to make shifting a just right experience.

    Packers and Movers Gurgaon
    Packers and Movers Mumbai
    Packers and Movers Pune
    Packers and Movers Bangalore

    ReplyDelete
  22. Generally I don't read anything but thanks for writing when i read this i felt how graceful as a writer you are.thank you for posting-

    Packers And Movers Indore
    Packers And Movers Baoliakhurd Indore
    Packers And Movers Barlai Jagir Indore

    ReplyDelete
  23. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings.
    Packers And Movers Pune

    ReplyDelete
  24. a very amazing i love it thanks for information obat ambeien

    ReplyDelete
  25. i cannot truly enable but admire your weblog, your weblog is so adorable and great.It has given me courage to try scarier things. I tend to steer clear of them but not anymore.
    Packers And Movers Lucknow

    ReplyDelete
  26. once the website is really inspiring, I wish it good luck, the more cutting edge and always provide information that is useful, familiar greeting from our best link
    obat mata rabun

    ReplyDelete
  27. http://acemaxs31.com/obat-maag/
    http://acemaxs31.com/obat-herbal-infeksi-saluran-kemih/
    http://qr.net/bUbHk

    ReplyDelete
  28. cara pemesanan slimming capsule
    http://acemaxs31.com/cara-mengecilkan-perut-buncit/
    http://acemaxs31.com/obat-herbal-pelangsing/
    http://acemaxs31.com/cara-cepat-melangsingkan-tubuh-secara-tradisional/
    http://acemaxs31.com/obat-herbal-pengurus-badan/
    http://acemaxs31.com/cara-melangsingkan-tubuh-secara-alami-dan-cepat/

    ReplyDelete
  29. Hii you are providing good information.Thanks for sharing if anyone interested The Best Hardware and Networking Institute in Hyderabad from RCP Technologies with Industry experts. See below


    hardware-and-networking training

    ReplyDelete