Hands On 27

By Simon Free (Bio)

In this hands on, you are going to add caching to the blog and to the portfolio sections.

Tags Used: <cfcache>, <cfset>

Functions Used: sleep, cacheGet, isNull, cachePut

  1. 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/.
  2. Once logged in, go to 'Debugging Output Settings' under Debugging & Logging.
  3. Check the 'Enable Request Debugging Output' option and click 'Submit Changes'.
  4. Open up the /www/blog.cfm page in your browser.
  5. Scroll to the bottom of the page and take note of the additional information being displayed.
  6. Open up the /www/blog.cfm file in your code editor.
  7. 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)#
  8. Place the closing </cfcahe> tag after the closing </cfoutput> tag.
  9. Reload the /www/blog.cfm page in your browser. Take note of the execution time.
  10. 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.
  11. 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.
  12. 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.
  13. Create a <cfcache> tag with the following attribute:
    • action: flush
  14. In your browser, go to the /www/clearcache.cfm page. You will not see any output but the cache is now cleared.
  15. Go back to the /www/blog.cfm page in your browser and refresh. Take note of the execution time.
  16. 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.
  17. 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) />
  18. 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.
  19. 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.
  20. 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.
  21. Remove the sleep tag we just entered.
  22. 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.
  23. At the top of the page you should have a <cfquery> tag. Prior to this, create the following <cfset> tag:
    <cfset myPortfolio = cacheGet('myPortfolio') />
  24. 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)>
  25. Move the <cfquery> that was already in the file inside the <cfif> tag.
  26. 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">
    	<cfset cachePut('myPortfolio',myportFolio) />
  27. 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.