CFQUERYPARAM, Part Deux

Cfcoder made a good point about why some people (particularly novices) probably don't use CFQUERYPARAM, and that is that if you use CFQUERYPARAM, you can't use the CACHEDWITHIN or CACHEDAFTER attributes of the CFQUERY tag.

That being said, I don't think this is necessarily a valid point. In general, optimization should be the last thing on your mind when you're coding. If you need to cache something as granular as a query, you shouldn't even consider the need until the application has been written, and it's been demonstrated that caching that query will remove a performance bottleneck that actually affects the application.

By and large, novice programmers are not going to be working on applications of sufficient size that such performance tweaks will be necessary. If they are, and it truly is a novice programmer, chances are good that the real problem lies in the application's design at a much higher level. If that's not the case, it is amazingly trivial to cache information (including, but not limited to, queries) in any of the shared scopes that CF exposes. Any novice that assumes skipping CFQUERYPARAM for reasons of caching is merely confirming their inexperience. I'll freely admit to arriving at that conclusion at one point in my career, but not any more.

A much better route is to place the query into a reusable module, be it a UDF, an include, a CFC method, or even a custom tag. Within that module, you can set up and use a custom caching mechanism, which could range from a simple shared-scope variable, to utilizing a shared-scope CFC instance that manages a cache, to an advanced caching framework. If your query is already in a CFC method or an include (such as a Fusebox fuse), then you're 90% of the way there.

Comments are closed.