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.

<cfcache action="cache" timespan="#CreateTimeSpan(0,0,0,30)#" idletime="#CreateTimeSpan(0,0,0,5)#">

The current date/time is: #Now()#

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.

Aaron West's Gravatar
About this post:

This entry was posted by Aaron West on November 21, 2009 at 8:23 PM. It was filed in the following categories: ColdFusion. It has been viewed 11180 times and has 3 comments.

13 related blog entries

3 Responses to 14 Days of ColdFusion 9 Caching: Day 5 - The Difference Between Timespan and Idletime

  1. Aaron, if you have addressed this in further posts, just say so; but, I am having some trouble coming up with a reason to use TimeSpan vs. IdleTime. I mean, I understand the difference, but I'm not sure I see how / why to choose one over the other (or when to use both)?

    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.

  2. @Ben - Sorry for the late reply on your question. If I had to guess I'd say using timespan and idletime at the same time /is/ the least likely of the three scenarios. I too haven't though too much about a real-world scenario for using both attributes at the same time, but I'm sure one exists. Just think of a situation where you'd want an absolute timeout of the cache (timespan) and an "unread" or "not looked at in a while" (idletime) timeout.

    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.

  3. @Aaron,

    No worries my man; I know how it is. I like your shopping cart scenario - that makes sense.