The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Free Code! - querycache.cfc

posted under category: ColdFusion on October 4, 2004 at 12:00 am 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 Dopefly.com 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.