Before I jump into today's ColdFusion 9 caching content I want to bring up yesterday's post about flushing pages from cache. Rob Brooks-Bilson, long-time ColdFusion community contributor and all around great guy mentioned I had neglected a critical part of my post. I had left off the useQueryString attribute when caching the page which would cause ColdFusion to cache the template once regardless of the URL parameters passed in. I set out to "fix" my post and jumped into a ball of confusion that in the end chewed up hours of my day. Long story short, if you read yesterday's post you need to read it again. I wound up rewriting most of the post in order to explain what I feel is a bug in ColdFusion 9.0.
Today, I want to talk about the difference between a cache timespan and idletime and explain how they work together. Here's the sample code for today.
<cfoutput>
The current date/time is: #Now()#
</cfoutput>
The timespan and idletime attributes can be set to the result of the CreateTimeSpan() function as shown here, or you can set them to a decimal number of days such as 5 (5 days), 1.5 (1.5 days) or .25 (1/4 day). In this example, the page cache will never last longer than the 30 second timespan, but could last as short as 5 seconds if the cache receives no hits in 5 seconds. This means the timespan attribute allows you to set how long an item will be cached regardless of how often that item is accessed and retrieved from cache. In contrast, the idletime attribute says, keep this item cached as long as it is accessed at least once every x, where x is the value of idletime. Taken together these two attributes give developers a lot of control over how items persist (or not) in cache.
I believe this granular control of cache lifespan will not only be great for developers, but will cause you to think a bit. Where I work, we have some pretty serious caching strategies in play. At any one time we have several thousand Applications (ColdFusion app framework) running per server. Some of these Applications receive a lot of traffic while others don't. Given the Windows limitation on the size of the JVM memory, we have to think really hard about what we store in shared scopes. Not only how much we store, but for how long. We have really snazzy homegrown utilities that troll through all Applications on all servers looking for stale data and inactive sites. When this sort of data or level of inactivity is found we execute cache clearing code that removes data from shared scopes.
With timespan and idletime together, we won't have more control over what we are already doing, but we'll have an easier time doing it.
The takeaways from this post are:
- the timespan attribute allows you to set how long an item will be cached regardless of how often that item is accessed and retrieved from cache
- the idletime attribute says, keep this item cached as long as it is accessed at least once every x, where x is the value of idletime
- idletime can be greater than timespan, but the timespan value will govern the cache lifespan
- you can use the idletime attribute without the timespan attribute. The default server timespan of 1 day (86400 seconds) would be the overall lifespan of the item in the cache
Click here to download the code mentioned in this post.














I can see that IdelTime would be good for very *rarely* hit pages if you want to clear it from memory if no one sees it. And I can see TimeSpan for use with things that change often, but not too often (a sort of delayed freshness).
But, I cannot really think of a good use of both at the same time. Is this the least likely of the three combinations?
This caching stuff is so interesting - I do so little of it, I am definitely missing out.
Maybe a shopping cart is a decent example? You could cache the contents of a users shopping cart for a maximum of 5 days. So no matter what the user does, if they don't complete the checkout process in 5 days their shopping cart is deleted (removed from cache). In the meantime, they also must access their shopping cart every 24 hours. If they fail to log into the site and view the shopping cart every 24 hours this too will cause the cart to be deleted. Maybe this example is a stretch, but its one I just thought of.
No worries my man; I know how it is. I like your shopping cart scenario - that makes sense.