…posted by davidjmedlock:

I was reading up on some ColdFusion optimization techniques recently for work and I came across something that said you could increase the efficiency of caching your queries by eliminating the CreateTimeSpan() function used in the CachedWithin attribute of your cfquery tags.

Now, here’s my disclaimer: This is not really my idea. I saw it last week and for the life of me I can’t find it anywhere and I can’t remember where it was. (Seems like it was House of Fusion, but I didn’t see it in there unless I just missed it.) To the author who came up with it, if you read this let me know so I can give due credit. It’s pretty cool and I thought I’d share with our audience here.

CreateTimeSpan simply takes the days, hours, minutes, and seconds and converts them to a decimal number. This is based on days. So one day is 1, 1 day and 1 hour is 1.0416666666667 and so on. So, you can eliminate the function call to CreateTimeSpan() by doing a little junior high math on your own:

1 day: 1

1 hour: 0.0416666666667

1 minute: 0.000694444444444

1 second: 1.15740740741

If you want to figure out various fractions yourself, you just have to do this:

Hours: (h / 24) = timespan

Minutes (m / 24) / 60 = timespan

Seconds = (s / 24) / 60 / 60 = timespan

That will give you a number you can drop into cachedWithin to keep from having that additional function call. Whether this makes a noticeable difference, I can’t say. If you have a lot of cached queries, it just might, especially if they’re refreshed frequently. As with any optimization techniques, you’ll really start to notice the difference as your application grows, even though you may not see it in its infancy.

Correction:
To give credit where credit is due, this was actually on House of Fusion as I thought. It was actually on the one place I didn’t look: the front page. Under Optimization Overkill you’ll find more information. Thanks to author Michael Dinowitz for pointing it out.

Tags:

• Michael Dinowitz

It was something I wrote on the front of the House of Fusion site.

• http://www.morgankelsey.com nagrom

Good tip! Another method is to assign the value to a variable, and thus only run the function once.

I do something like this in my Application.cfm file this:

With either of these methods, you can also give yourself the option of running with no cache by using a URL var. (Make it something mysterious to keep people from slamming the server)

Can be a big help with debugging, and with client admins where they need to see instantaneous results. I like to make a cfhttp call to flush the page when the client performs an update.

### Recommended for you

Special Offer
Free course!

Git into it! Bonus course Introduction to Git is yours when you take up a free 14 day SitePoint Premium trial.