Simon Baynes: Web Development

Caching cfquery

To reduce overhead on your ODBC connection you can cache your database queries. Now what this actually means is that database queries that are unlikely to change can be stored in server memory and not checked again until the specific time period has elapsed. So say you have cached your query for 5 minutes and 100 people access that page in that 5 minutes your database is only accessed once. This has obvious benefits.

To make use of this, you need to understand the CreateTimespan() function. The function takes four variables which equate to days, hours, minutes, seconds.

CreateTimespan(0, 0, 5, 0); //would be 5 minutes.  

So to use that with a query:-

<cfquery name="qGetData" datasource="data" cachedwithin="#CreateTimespan(0, 0, 5, 0)>
    SELECT *
    FROM table  
</cfquery>

Use of this will really improve your application performance.

By Simon Baynes