The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Free Code! - querycache.cfc

posted under category: ColdFusion on October 4, 2004 by Nathan

download queryCache now.

QueryCache is a way to have more control over CFMX's default query caching mechanism. While CachedWithin and CachedUntil work great for static queries, what is the best way to cache a query that will change frequently? The answer is queryCache.cfc.

Instantiate the queryCache object like this:
<cfset queryCache = createObject("component","queryCache")>
(note: I store it in application scoped CFC's variables scope)

Cache a query by running this instead of a cfquery tag:
<cfset myLocalQueryItem = cachedQuery("getThings", "SELECT * FROM table", "myDSN",createTimeSpan(1,0,0,0))>
The first field is the cached query's unique name, the second is your SQL statement, 3rd, your DSN, and last, an optional timeout period.

Flush a single cached query like this:
<cfset cachedQuery("getThings")>
where the only parameter is the unique name of your query.

Other queryCache functions:
<cfset isQueryCached("getThings")>
<cfset getCachedQueryKeys()>

Special features:
Cached a query based on a timeout or until it is explicitly deleted.
If a re-connection to the database fails, queryCache uses the latest cached query it has in memory.

QueryCache has been running on flawlessly for over a month. I originally created it because my database server is up only half the time, and I wanted to improve performance. I am distributing the code free of charge with no license. Use it at your own risk, i'm not responsible for anything that happens when you use it.

download queryCache now.

Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.