In Phoenix Next Week? Come See a Preview of My cf.Objective Caching Presentation

If you happen to be in the Phoenix area on Wednesday, January 25th, stop on by the University of Advancing Technology at 6:30 pm to catch a preview of my upcoming cf.Objective presentation: Ten Steps to Move Your Use of Caching Beyond the Basics. Here's a short description of the presentation:

We all know that caching plays an important part of any performant and scalable application architecture and that with ColdFusion 9 caching is easier than ever. This session explores 10 steps you can take to move your use of caching beyond what ColdFusion provides out of the box.

*Please note that this presentation won't be recorded/streamed. You have to be there in person to see it. Normally I'm all for recording and streaming, but since this is a preview of a talk I'll be giving at an upcoming conference, I want to limit the audience out of respect to those attending the conference.

User Defined Caches in Coldfusion 9.0 and 9.0.1

The follow up discussion to a recent blog post by Ray Camden made me realize that there's a lot of misunderstanding (including the ColdFusion documentation) about how ColdFusion's user defined caches work. In this blog post, I hope to clear up some of that confusion.

ColdFusion 9.0 completely overhauled CF's caching mechanism and replaced the old file based system with Ehcache. Out of the box, ColdFusion made two default caches available to every ColdFusion application, an object cache and a template cache. It was also possible to create additional user-defined caches at runtime, but only if you were using the cfcache tag, not the caching functions. Here's an example that puts and gets data from a user defined cache:

view plain print about
1<!--- attempt to get the artist query from the custom object cache --->
2<cfcache
3 key="myCustomObjectCache"
4 action="get"
5 id="artistQuery"
6 name="getArtists"
7 metadata="myMeta">

8
9<!--- if the item isn't there, it'll return null. In that case, run the query
10 and cache the results and rerun the data from the db instead --->

11<cfif isNull(getArtists)>
12 <cfquery name="getArtists" datasource="cfartgallery">
13 SELECT *
14 from artists
15 </cfquery>
16
17 <cfcache
18 key="myCustomObjectCache"
19 action="put"
20 id="artistQuery"
21 value="#getArtists#">

22</cfif>
23
24<!--- dump the query from cache --->
25<cfdump var="#getArtists#">

ColdFusion 9.0.1 added the ability to use user defined caches in many of the caching functions as well. Here's our previous example written using cacheGet() and cachePut() instead of the cfcache tag:

view plain print about
1<!--- attempt to get the artist query from the custom object cache --->
2<cfset getArtists = cacheGet('artistQuery', 'myCustomObjectCache')>
3
4<!--- if the item isn't there, it'll return null. In that case, run the query
5 and cache the results and rerun the data from the db instead --->

6<cfif isNull(getArtists)>
7 <cfquery name="getArtists" datasource="cfartgallery">
8 SELECT *
9 from artists
10 </cfquery>
11
12    <cfset cachePut('artistQuery', '#getArtists#', 15, 15, 'myCustomObjectCache')>
13</cfif>
14
15<!--- dump the query from cache --->
16<cfdump var="#getArtists#">

One thing to note here is that neither cacheGetProperties() nor cacheSetProperties() can be used to configure the properties for a custom cache in ColdFusion 9.0 or 9.0.1. If you want the cache configuration to differ from ColdFusion's default cache configuration, you'll need to configure the cache in your ehcache.xml file:

view plain print about
1<cache
2 name="myCustomObjectCache"
3 maxElementsInMemory="10000"
4 eternal="false"
5 timeToIdleSeconds="86400"
6 timeToLiveSeconds="86400"
7 overflowToDisk="false"
8 diskSpoolBufferSizeMB="30"
9 maxElementsOnDisk="10000000"
10 diskPersistent="false"
11 diskExpiryThreadIntervalSeconds="3600"
12 memoryStoreEvictionPolicy="LRU"
13 clearOnFlush="true"
14/>

Obviously it's not always desirable or even possible (say in the case of a shared hosting environment) to hard code configuration settings in the ehcache.xml file. Besides that, anytime you make changes to the XML file you need to restart ColdFusion for them to take effect. While I hope that Adobe adds the ability to set properties for custom caches in a future release of ColdFusion, there is a solution you can take advantage of today in the form of a small UDF I wrote called cacheCreate().

The cacheCreate() UDF allows you to create a new user defined cache at runtime and pass in configuration options that ColdFusion currently requires you to set in your ehcache.xml file. It uses Java to call the underlying Ehcache API and do the heavy lifting for you. Here's the code (you can also get cacheCreate() at cflib.org:

view plain print about
1<cffunction name="cacheCreate" output="false" returntype="void"
2    hint="I create a new user defined cache region in Ehcache"
3    description="I create a new user defined cache region in Ehcache. This function
4             allows you to also configure the attributes for the custom cache,
5             something you would normally have to hard code in the ehcache.xml
6             file if you rely on ColdFusion'
s built in caching functions. I named
7             the function cacheCreate() and not cacheNew() in the hopes that a
8             future version of ColdFusion includes a cacheNew() function with
9             similar functionality.">
10
11    <!--- this is what's configurable as of Ehcache 2.0 (CF 9.0.1). Only required
12         argument is Name --->

13    <cfargument name="
name" type="string" required="true">
14    <cfargument name="
maxElementsInMemory" type="numeric" default="10000">
15    <cfargument name="
maxElementsOnDisk" type="numeric" default="10000000">
16    <cfargument name="
memoryStoreEvictionPolicy" type="string" default="LRU">
17    <cfargument name="
clearOnFlush" type="boolean" default="true">
18    <cfargument name="
eternal" type="boolean" default="false">
19    <cfargument name="
timeToIdleSeconds" type="numeric" default="86400">
20    <cfargument name="
timeToLiveSeconds" type="numeric" default="86400">
21    <cfargument name="
overflowToDisk" type="boolean" default="false">
22    <cfargument name="
diskPersistent" type="boolean" default="false">
23    <cfargument name="
diskSpoolBufferSizeMB" type="numeric" default="30">
24    <cfargument name="
diskAccessStripes" type="numeric" default="1">
25    <cfargument name="
diskExpiryThreadIntervalSeconds" type="numeric" default="120">
26    
27    <!--- We need to do this in java because ColdFusion's cacheGetSession() returns
28     the underlying object for an EXISTING cache, not the generic cache manager --->

29    <cfset local.cacheManager = createObject('java', 'net.sf.ehcache.CacheManager').getInstance()>

30
31    <!--- constructor takes cache name and max elements in memory --->
32    <cfset local.cacheConfig = createObject("java", "net.sf.ehcache.config.CacheConfiguration").init("#arguments.name#", #arguments.maxElementsInMemory#)>

33    <cfset local.cacheConfig.maxElementsOnDisk(#arguments.maxElementsOnDisk#)>

34    <cfset local.cacheConfig.memoryStoreEvictionPolicy("#arguments.memoryStoreEvictionPolicy#")>
35    <cfset local.cacheConfig.clearOnFlush(#arguments.clearOnFlush#)>
36    <cfset local.cacheConfig.eternal(#arguments.eternal#)>
37    <cfset local.cacheConfig.timeToIdleSeconds(#arguments.timeToIdleSeconds#)>
38    <cfset local.cacheConfig.timeToLiveSeconds(#arguments.timeToLiveSeconds#)>
39    <cfset local.cacheConfig.overflowToDisk(#arguments.overflowToDisk#)>
40    <cfset local.cacheConfig.diskPersistent(#arguments.diskPersistent#)>
41    <cfset local.cacheConfig.diskSpoolBufferSizeMB(#arguments.diskSpoolBufferSizeMB#)>
42    <cfset local.cacheConfig.diskAccessStripes(#arguments.diskAccessStripes#)>
43    <cfset local.cacheConfig.diskExpiryThreadIntervalSeconds(#arguments.diskExpiryThreadIntervalSeconds#)>
44
45    <cfset local.cache = createObject("java", "net.sf.ehcache.Cache").init(local.cacheConfig)>
46    <cfset local.cacheManager.addCache(local.cache)>
47</cffunction>

To create and configure a new user defined cache in your application, all you need to do is something like this:

view plain print about
1<!--- let's build a struct of arguments. It's much easier to pass this way --->
2<cfset myProps = structNew()>
3<cfset myProps.name = "myCustomCache">
4<cfset myProps.maxElementsInMemory = 10>
5<cfset myProps.maxElementsOnDisk = 10>
6<cfset myProps.memoryStoreEvictionPolicy = "FIFO">
7<cfset myProps.clearOnFlush = true>
8<cfset myProps.eternal = false>
9<cfset myProps.timeToIdleSeconds = 86400>
10<cfset myProps.timeToLiveSeconds = 86400>
11<cfset myProps.overflowToDisk = false>
12<cfset myProps.diskPersistent = true>
13<cfset myProps.diskSpoolBufferSizeMB = 30>
14<cfset myProps.diskAccessStripes = 1>
15<cfset myProps.diskExpiryThreadIntervalSeconds = 120>
16
17<!--- create the new custom cache region --->
18<cfset cacheCreate(argumentCollection=myProps)>
19
20<!--- prove that it's there the CF way --->
21<cfdump var="#cacheGetSession('myCustomCache', 'true').getCacheManager().getCacheNames()#">

While this won't work in all cases (say you want to cluster your cache or add in a Terracotta Server Array), it should get you thinking about other ways you can extend ColdFusion's ehcache implementation by utilizing some of the Ehcache Java API within CF.

Thoughts on the Software AG Acquisition of Terracotta

As you might have heard, Terracotta -- the makers of Ehcache, the Terracotta Server Array, Quartz Scheduler and Big Memory recently announced their intent to be acquired by German software company Software AG. If you're wondering who Software AG is, you're probably not alone. Until recently, Software AG was a relatively little known company in the US. Although they've been in business since the late 1960's, their main market had been Europe. That all changed in 2007 when they acquired WebMethods, one of the largest providers of integration systems and services. This more than doubled their North American customer base. They continued to fuel their rapid growth from their mainly through acquisition. After WebMethods came IDS Scheer in 2009, Data Foundations in 2010, and now Terracotta. Each of their acquisitions has succeeded in growing their customer base, and in most cases, their revenue as well.

So, why Terracotta when Software AG is known as a business process and SOA infrastructure company? The first reason is for the technology itself, mainly Ehcache, Terracotta, and Big Memory. Software AG has big plans for the cloud, and in-memory capability and scalability play a huge part in building out their PaaS infrastructure. There are other potential benefits as well. Open source Ehcache claims an install base of over 500,000 with a sizeable open source community to back it. Although Software AG has little experience in the open source market, they've committed to the Ehcache and Quartz communities going forward. There's also the opportunity to improve job scheduling within the existing WebMethods suite. If you've ever used their existing scheduler, you know that it leaves much to be desired.

The company I work for has been a Software AG customer for close to 7 years now. We started with their ESB (Crossvision Service Orchestrator) and Tamino XML database, upgraded to WebMethods Integration Server after the merger, and have added capabilities around Business Activity Monitoring and SOA Governance since then. All along the way, Software AG has worked closely with us to meet our often challenging requirements. I think in that respect, Terracotta customers are in good hands. I've also spent considerable time working with Ehcache and Terracotta over the past 3 years now – writing articles and blog posts about their caching technology as well as speaking at several conferences. Although I didn't see the acquisition coming, I think combining the passion and talent of the folks at Terracotta with the resources and track record of Software AG will be a win for everyone involved.

While that last statement might sound like rainbows and unicorns, there is one area where I hope Software AG can learn from Terracotta, and that's community. Having been involved with various technology communities over the years, I feel that the Software AG community could use some help. With the exception of a few sites, there's little evangelism that happens outside of Software AG branded properties. Sure there are official Software AG blogs, and Software AG forums, but what I don't see is a vibrant community outside of Software AG writing about and talking about their products. Contrast that with Terracotta, and especially the Ehcache community and you'll see exactly what I mean. Let's hope that Software AG makes the most of the community they're getting along with the technology.

Slides, Config and Code from my cf.objective() 2011 Presentation

Slides, config files and code for my cf.objective() 2011 presentation, "Undocumented and Off Script: ColdFusion & Ehcache" are now available. You can view the slides inline, or download the attached zip file which contains the presentation and all files/code from the presentations.

As always, if you have questions or want to discuss, leave a comment or hit me up on twitter at @styggiti.

Speaking at cf.Objective 2011: Undocumented and Off Script: ColdFusion & Ehcache

Headed to cf.Objective 2011 and want to learn more about caching in ColdFusion with Ehcache? Stop by my session Undocumented and Off Script: ColdFusion & Ehcache on Thursday, May 12th at 10:15 am. I'll cover a broad range of topics including upgrading the version of Ehcache that ships with ColdFusion, clustered caching, distributed caching, cache search, cache monitoring, developer tools, and much, much more.

I hope to see you there!

Building High Performance Applications with ColdFusion 9 and Ehcache 2.4 on DZone

My latest article on Building High Performance Applications with ColdFusion 9 and Ehcache 2.4 is out on DZone. In addition to covering basic caching concepts, the article also walks you through the process of updating the version of Ehcache that ships with Coldfusion from 2.0 to 2.4 as well as installing and configuring a Terracotta server for distributed caching.

Recording, Slides and Demo Code now Available from my ColdFusion/Ehcache Webinar

The Webex recording, Powerpoint slides and example code I used in last week's Boosting Performance and Scale for ColdFusion Applications with Ehcache webinar are now available. You can watch the recording here (note that registration is required):

Boosting Performance and Scale for ColdFusion Applications with Ehcach

Additionally, you can view the presentation slides:

Alternately, you can download the slides.

You can also download the sample code.

Please note that the examples created for blogCFC should only be considered a POC to demonstrate concepts and not production ready code.

Issue with Ehcache and ColdFusion Query Objects

I submitted a bug for this but the system seems to have swallowed it up without giving me the bug number (probably because I submitted as cf 9.0.1).

I've found what I consider to be a serious bug an issue with ColdFusion's ehache implementation and query objects that may bite you if you're not aware of how it works. If I take a query object and stick it in cache, then perform an operation on the original query object such as adding a column or performing certain query of query operations, ColdFusion is treating the query object that's in cache as if it were a copy by reference to the original query object. That is, if I make a change to the original query object, ColdFusion is also applying the change to the version that's in cache.

As far as I'm concerned, cache represents a boundary that ColdFusion should not implicitly cross. With most caching systems I've worked with in the past (such as memcached), the cache always acted as a dumb key/value store. Unless I perform an explicit cachePut(), I wouldn't expect that CF would update values in the cache. Here's two code snippets that reproduces the case. The first uses the cfartgallery data source:

view plain print about
1<!--- Demo code to show that ColdFusion's passig query values by reference is crossing boundaries and updating a query stored in cache --->
2<cfquery name="getArtists" datasource="cfartgallery">
3select *
4from artists
5</cfquery>
6
7<!--- dump the original query --->
8<cfdump var="#getArtists#">
9
10<!--- put the query in cache --->
11<cfset cachePut("artistsQuery", getArtists,
12'#createTimeSpan(0,0,5,0)#', '#createTimeSpan(0,0,5,0)#')
>

13
14<!--- add a column to the query --->
15<cfset queryAddColumn(getArtists,"newColumn",arrayNew(1))>
16<cfdump var="#getArtists#">
17
18<!--- clear the query --->
19<cfset getArtists="">
20<cfdump var="#getArtists#">
21
22<!--- get the query from cache again - note that the added column is
23there! --->

24<cfdump var="#cacheGet("artistsQuery")#">

The second example shows how this can be done with a query of a query, based on a different bug Ray found previously (see http://www.coldfusionjedi.com/index.cfm/2009/8/28/Another-example-of-the-QofQ-Bug). In this sample, note that the date gets reformatted by the query of a query and both the original query and the cached version get updated:

view plain print about
1<cfset mydata = queryNew("mydate, rannum")>
2
3 <cfloop index="loop1" from="1" to="5">
4 <cfset newrow = queryaddrow(mydata, 1)>
5 <cfset temp = querysetcell(mydata, "mydate", #dateformat(now()-
6loop1,"dd-mmm-yy")#, #loop1#)
>

7 <cfset temp = querysetcell(mydata, "rannum", 55.65, #loop1#)>
8 </cfloop>
9
10<cfdump var="#mydata#">
11
12<cfset cachePut("myQuery", myData, '#createTimeSpan(0,0,5,0)#',
13'#createTimeSpan(0,0,5,0)#')
>

14
15<cfdump var="#mydata#">
16
17<cfquery dbtype="query" name="query4graph">
18 select mydate, rannum from mydata
19 </cfquery>
20
21 <cfdump var="#query4graph#">
22
23 <cfdump var="#cacheGet("myQuery")#">

The work around for this problem is to use the duplicate() function to make a clone of the query object before doing the cachePut(). Although this works, there are other potential consequences from having two copies of the query around, so be careful.

Update: It looks like this is actually expected behavior in Ehcache. Unfortunately, it's not documented in the ColdFusion documentation anywhere, but Ehcache actually has two configurable parameters (as of v. 2.10) called copyOnRead and copyOnWrite that determine whether values returned from the cache are by reference or copies of the original values. By default, items are returned by reference. Unfortunately we can't take advantage of these parameters right now as CF 9.0.1 implements Ehcache 2.0.

I can live with this, but it's not what I expected as I've always viewed Ehcache as a "dumb" key-value store and certainly didn't expect this behavior. Even if ColdFusion was running v 2.10 of Ehcache, it's still not something we could easily configure. Since ColdFusion currently doesn't have a cacheNew() function for creating new user-defined cache regions, the only way to turn this functionality off would be to hard-code your cache configuration in your ehcache.xml file for each user-defined region where you want to disable copyOnRead and copyOnWrite.

Retrieving a List of Active Ehcache Regions in ColdFusion 9.0.1 and a Potential Gotcha

ColdFusion 9.0.1 added a new function called cacheGetSession() that returns the underlying cache object for Ehcache. What's really cool about this is that it allows you to get at just about all of the features available in Ehcache that aren't directly exposed to ColdFusion through built-in functions.

I was recently working on some example code for an upcoming caching presentation when I ran into a situation where I wanted to get a list of all caches that had been created on the server, including custom caches I had created myself. One simple line of code is all it took:

view plain print about
1<cfset cacheNames = cacheGetSession('object').getCacheManager().getCacheNames()>
2
3<cfdump var="#cacheNames#">

What you should note here is that even though I'm telling cacheGetSession() to get the cache object for the default object cache, getCacheManager() exposes a method called getCacheNames() which returns an array of cache names for the instance of cache manager. In the case of ColdFusion's Ehcache implementation, ColdFusion instantiates cacheManager as a singleton such that all caches are managed by the single cache manager. When you call getCacheNames(), it will return you a list of all caches currently implemented on the instance of ColdFusion.

While this may not be an issue for many people, if you are on a shared server it may be a concern. The only way to disable this is to disable the cacheGetSession() function using sandbox security.

Announcement: Boosting Performance and Scale for ColdFusion Applications with Ehcache (Webinar)

If you've been following my series on caching in ColdFusion 9, you know that ColdFusion 9.0 includes the powerful caching platform Ehcache from Terracotta. What you may not know, though, is that in ColdFusion 9.0.1 Adobe upgraded the Ehcache engine from version 1.6 to 2.0. This brings several new caching capabilities to ColdFusion and makes them dead simple to implement. If you're interested in learning more, Mike Allen from Terracotta and I are presenting a webinar on August 12, 2010 at 11am Pacific time. Here's the official announcement:

Join me and Mike Allen, the head of product management at Terracotta in a discussion about simple and effective ways to scale and boost the performance of ColdFusion applications using Ehcache.

In this webcast, you'll learn:

  • Why caching is important
  • How ColdFusion leverages Ehcache to boost performance
  • Tips and tricks to leverage the newest features of Ehcache in ColdFusion
  • How to take advantage of the ability to snap in Enterprise Ehcache to scale out your applications

Please register for this webcast even if you are unable to attend and you will get an email with a link to the recording.

Register for the webinar