One of the less mentioned aspects of ColdFusion 9 is the enhanced caching that was added by including ehcache under the covers.
This opens a number of possibilities including fragment caching:
The time was #DateFormat(Now(), "mmmm d, yyyy")#
#TimeFormat(Now(), "hh:mm:ss tt")#
The time is #DateFormat(Now(), "mmmm d, yyyy")#
#TimeFormat(Now(), "hh:mm:ss tt")#
In this code, the first <cfoutput> will always show the time from the first time it was called. The second <cfoutput> will show the time from the actual time the code is called. The 1 for timespan means that it will cache that value for a day.
But, we can also do dependent cached items, where the value of one of the contained variables has an impact on whether or not the cached item needs to be refreshed.
The minutes is #minutesVariable#
The time was #DateFormat(Now(), "mmmm d, yyyy")#
#TimeFormat(Now(), "hh:mm:ss tt")#
In the above code, assume that it is called at 12:03. For the next minute minutesVariable is going to equal 3. For each call where minutesVariable equals 3 the cache is used. However when the time rolls over to 12:04, minutesVariable will equal 4. This will trigger a refreshing of the cache with the new content being cached for the next minute.
In addition to fragments, I can also cache objects (but for easy of understanding, variables) See this code:
SELECT *
FROM Artists
I try and retrieve the query that I’ve given an id of “testQuery” from cache, if it’s not there, I call the query and cache it.
So those are three fairly straight forward examples of the new caching, but you can do much more with it, including invalidating objects, analyzing cache usage, and more.