chapter31

development in a land far far away…

at the moment

History is not what happened. History is what was written down.

Stored procedures are great for many reasons but one of the reasons I might not use them is you can’t cache them…or can you?

A discussion at work enlightened me to a simple way around the lack of the cachedwithin attribute inside the <cfstoredproc> tag. Simply use the <cfquery> tag instead!

Ok, so normally if I wanted to use a stored procedure I might do something like:

But did you know that you can execute your stored procedures from within the <cfquery> tag? Well you can, that way you can also take advantage of the cachedwithin attribute!:

One thing though is that this will only work when you return a single record set, if you need to return multiple record sets (which I often do) then <cfstoredproc> is your only option as you can use the <cfprocresult> tag like:

It always amazes me when you get simple solutions to problems you have using what you already know in a creative way, at least creative or new to you!

Always look to refactor your code, you never know what you’ll learn to save you time and effort in the future :)


Related Pages

3 Responses to “Caching stored procedures”

  1. [...] You can have ColdFusion cache the query - which it doesn’t do with stored procedures. Update Nov 2006: you can actually cache stored procedures! See here for more info [...]

    chapter31 » cfstoredproc vs cfquery

  2. I tried this technique for caching stored procedures. It just about halved the time of pages on our web site. Thanks for this! There was one big SP that was slowing the whole boat down. Caching it was key!

    Nate

  3. Excellent news Nate! Glad it helped. I think too many people underestimate the power of caching…all kinds of caching.

    Michael Sharman

Leave a Reply