In this hands on, you are going to add caching to the blog and to the portfolio sections.
- Before you add caching, turn on the debugging information of the page so that the changes we are about to make can be detected. Login to your ColdFusion administrator. The ColdFusion Administrator will be located at http://localhost:8500/CFIDE/administrator/.
- Once logged in, go to 'Debugging Output Settings' under Debugging & Logging.
- Check the 'Enable Request Debugging Output' option and click 'Submit Changes'.
- Open up the /www/blog.cfm page in your browser.
- Scroll to the bottom of the page and take note of the additional information being displayed.
- Open up the /www/blog.cfm file in your code editor.
The first thing you need to do is create a <cfcache> tag. Locate the Blog Posts comment and create a <cfcache> tag on the line below it with the following attributes:
- action: cache
- timespan: #createtimespan(0,1,0,0)#
- Place the closing </cfcahe> tag after the closing </cfoutput> tag.
- Reload the /www/blog.cfm page in your browser. Take note of the execution time.
- Refresh the page again and take note of the execution time. If this were a larger page, you would have noticed a decrease in the execution time.
- One problem right now is that even though the output is being cached, all blog posts are being pulled at the beginning of the page request. To rectify this, move the <cfset> located on line 1 so that it is inside the <cfcache> tag.
- If you were to run the page now, the output would still be cached as the cache will not expire for an hour. To clear the cache, call the <cfcache> tag with an action value of flush. Create a new page in the /www/ folder and call it clearCache.cfm.
Create a <cfcache> tag with the following attribute:
- action: flush
- In your browser, go to the /www/clearcache.cfm page. You will not see any output but the cache is now cleared.
- Go back to the /www/blog.cfm page in your browser and refresh. Take note of the execution time.
- Refresh the page again and compare the two execution times. If the page took long enough to run, you will see an improvement in the execution times.
To make the differences more visible, let's slow down the page load. Right after the opening <cfcache> tag, add the following line of code:
<cfset sleep(500) />
- The line of code you added will have ColdFusion wait for 500 milliseconds before it continues. Before testing this functionality, go back to the clearcache.cfm file in your browser and reload the page.
- Now that the cache is cleared, go back to the blog.cfm file in your browser and reload it. Take note of the execution time.
- Refresh the blog.cfm page and review the execution time. You will notice that the execution time is drastically different. This is because the cached content is being called, rather than processing the block of code.
- Remove the sleep tag we just entered.
- Now that the <cfcache> tag is in place, lets look at some programmatic caching. Open up the /www/portfolio.cfm file in your code editor.
At the top of the page you should have a <cfquery> tag. Prior to this, create the following <cfset> tag:
<cfset myPortfolio = cacheGet('myPortfolio') />
The cacheGet function pulls the cache from memory that has the identifier of myPortfolio. Right now, there is nothing cached with that identifier. First check if anything was returned; if not, you need to execute the code and store the result in cache. After the <cfset> tag, create a <cfif> tag, which will check if the myPortfolio variable is null. The code should look similar to this:
<cfif isNull(myPortfolio)> </cfif>
- Move the <cfquery> that was already in the file inside the <cfif> tag.
After the query, create a <cfset> tag that calls the cachePut function, passing in the string 'myPortfolio' as well as the query object called myPortfolio. The resulting code block should look similar to this:
<cfset myPortfolio = cacheGet('myPortfolio') /> <cfif isNull(myPortfolio)> <cfquery name="myPortfolio"> SELECT id, title, summary, website, image FROM portfolio </cfquery> <cfset cachePut('myPortfolio',myportFolio) /> </cfif>
- The myPortfolio query will now get cached if it is not already in cache. Run the page a few times and compare the execution times. If you want to clear the cache, reload the /www/clearCache.cfm page.