• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Caching a Query

Caching a query—that is, saving it to memory—will help speed up your application. By saving the results of your query to the server's memory, you save the ColdFusion Server from making a trip to the database to retrieve that information in the future. This is useful when the information in the query will not be changing frequently.

1.
In the firstquery.cfm file from the previous task, add the attribute cachedwithin to your <cfquery> tag.

<cfquery name="productlist" datasource="products" cachedwithin=””> 
SELECT *
FROM tblProducts
</cfquery>

By using the cachedwithin attribute you are telling the server to save the query. In the next step, you'll learn how to tell the server to keep the query cached for a specified timeframe.

2.
In the cachedwithin attribute, type #CreateTimeSpan(1,0,0,0)#.

Your code should now read:

<cfquery name="productlist" datasource="products" 
cachedwithin="#CreateTimeSpan(1,0,0,0)#">
SELECT *
FROM tblProducts
</cfquery>

The createtimespan function tells how long to cache the query. In this case, the query is being cached for one day. Createtimespan can specify a time in days, hours, minutes, or seconds.

Once the timeframe has passed, the server will rerun the query and save it to memory again.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint