Related Documentation : Integrations : Using Ehcache with ColdFusion : Example Integration
Example Integration
To integrate Ehcache with ColdFusion, first add the Ehcache jars to your web application lib directory.
The following code demonstrates how to call Ehcache from ColdFusion. It will cache a ColdFusion object in Ehcache and set the expiration time to 30 seconds. If you refresh the page many times within 30 seconds, you will see the data from cache. After 30 seconds, you will see a cache miss, then the code will generate a new object and put it in cache again.
<CFOBJECT type="JAVA" class="net.sf.ehcache.CacheManager" name="cacheManager">
<cfset cache=cacheManager.getInstance().getCache("MyBookCache")>
<cfset myBookElement=#cache.get("myBook")#>
<cfif IsDefined("myBookElement")>
<cfoutput>
myBookElement: #myBookElement#<br />
</cfoutput>
<cfif IsStruct(myBookElement.getObjectValue())>
<strong>Cache Hit</strong><p/>
<!-- Found the object from cache -->
<cfset myBook = #myBookElement.getObjectValue()#>
</cfif>
</cfif>
<cfif IsDefined("myBook")>
<cfelse>
<strong>Cache Miss</strong>
<!-- object not found in cache, go ahead create it -->
<cfset myBook = StructNew()>
<cfset a = StructInsert(myBook, "cacheTime", LSTimeFormat(Now(), 'hh:mm:sstt'), 1)>
<cfset a = StructInsert(myBook, "title", "EhCache Book", 1)>
<cfset a = StructInsert(myBook, "author", "Greg Luck", 1)>
<cfset a = StructInsert(myBook, "ISBN", "ABCD123456", 1)>
<CFOBJECT type="JAVA" class="net.sf.ehcache.Element" name="myBookElement">
<cfset myBookElement.init("myBook", myBook)>
<cfset cache.put(myBookElement)>
</cfif>
<cfoutput>
Cache time: #myBook["cacheTime"]#<br />
Title: #myBook["title"]#<br />
Author: #myBook["author"]#<br />
ISBN: #myBook["ISBN"]#
</cfoutput>
Copyright © 2014 Software AG, Darmstadt, Germany.

Product Logo |   Feedback