I've done well to get each of these caching blog posts written the night before they were "due" and have them scheduled for an 8:00am release. Not today. Last night I was incredibly tired and decided to wait until today to get this one out. Here it is after 9:00pm and I'm just now getting to it. Oh well, fasten your seatbelt folks because here we go!
In ColdFusion 9 you have access to metadata for object level caches only. I first demonstrated this metadata when we reviewed the cacheRemove() function. Today I want to spend time on two specific parts of this metadata, cache hits and cache misses which are defined as follows:
cache hit - cache is checked for a desired datum. If the datum exists it is retrieved
cache miss - cache is checked for a desired datum but it does not exist. Datum is retrieved from main data store such as a database
Let's take a look at cache hits and misses by looking at the following code:
<cfset cacheItem2 = cacheGet('wt-8-item2')>
<cfset cacheItem3 = cacheGet('wt-8-item3')>
<cfset cachePut('wt-8-item1', 'This is the cache for item 1.')>
<!--- Section A. Uncomment for step 2.
<cfset cachePut('wt-8-item2', 'This is the cache for item 2.')>
<!--- Section B. Uncomment for step 3. --->
<!---<cfset cachePut('wt-8-item2', 'Cache item 2 has been updated!')>--->
Cache Item 1:
Cache Item 2:
There's obviously nothing complicated here so let's look at the code in a few steps and see how different scenarios create different results in a browser.
To start, either copy the code above and paste it into a new template or use the link at the bottom of this post to download all the code in this series. Next, run the template as-is. On first run, notice how the cache_misscount has a value of three. This is due to the first three lines of code that attempt to retrieve three cached items. None of these caches exist yet hence all the cache misses. Also notice how the hitcount for cache item 1 is zero. The first time we run the template we create the cached item which sets the hitcount to zero. Subsequent refreshes of the page will increment the hitcount by one. Go ahead and press refresh a couple of times to see this effect. Here's a screen shot of what I saw the first time I ran the page.
Uncomment Section A and run the template again. This time, cache item 1 exists so its hitcount is incremented by 1. The second cacheGet() call for cache item 2 results in a cache miss which causes the cache_misscount to increase by one to four. The third cacheGet() call for cache item 3 also results in a cache miss incrementing cache_misscount to five. Now that Section A is uncommented, the conditional statement runs and determines that cache item 2 doesn't exist. The cachePut() function then executes and creates cache item 2 for the first time. Since it now has metadata the <cfdump> tag has something to display and you see the hitcount for cache item 2 set to zero. If you were to refresh the page over and over you'd see the hitcount for both cache item 1 and 2 increment by one with every page load. The overall cache_misscount will also increase by one since the cacheGet() for item 3 is still in the code. Here's the output shown on my screen when I run the code for step 2 just after step 1. Note, that I did not refresh the page a bunch during step 1 even though I mentioned you could. So, your results may not look exactly like mine.
Uncomment Section B so the cachePut() function for cache item 2 will run with every page load. Run the template and view the <cfdump> for this cached item. Notice how the hitcount value is zero and the lastupdated property is populated with a date/time. If you press the refresh button over and over this will continue. Each time you run the template ColdFusion is updating the already stored cache item which resets the hitcount to zero and puts the current server date/time in the lastupdated property. If you glance up at the <cfdump> for cache item 1, you'll see it's hitcount continue to increase with each refresh. Here's what the output looked like for me after uncommenting Section B and running the page once.
Now, comment Section B so it's back to the original state where the code won't run. Refresh the template and see what happens. The hitcount for cache item 2 begins to increment again while the lastupdated date/time stays the same from the last time the cachePut() ran in step 3. The lasthit property will also change with every request since a) the item is stored in cache and b) a cacheGet() for item 2 is running on every page refresh. After a bunch of refreshes here's what the two cfdump's looked like in my browser.
These examples are certainly elementary but they illustrate a few key points with caching in ColdFusion 9.
- each time you perform a cacheGet() and the item isn't in memory, ColdFusion will return null and the application specific cache_misscount will increment by one
- the first time a cachePut() places an item into object cache, the item hitcount is set to 0
- performing a cachePut() on an item already in cache will set the cache hitcount to 0 and will store a date/time value in the lastupdated property of the caches metadata
- each time an existing cached item is retrieved with the cacheGet() function, the hitcount property and application specific cache_hitcount property are incremented, and the lasthit property is updated with the current server date/time
Click here to download the code mentioned in this post.
NOTE: The code for this example was originally created by Rob Brooks-Bilson and was included in his Adobe MAX 2009 presentation. I've altered it some for the purposes of this blog post.
About this post:
This entry was posted by Aaron West on November 24, 2009 at 9:22 PM. It was filed in the following categories: ColdFusion. It has been viewed 6731 times and has 0 comments.
13 related blog entries
- 14 Days of ColdFusion 9 Caching: Day 14 - Setting the Server Cache Properties (November 30, 2009)
- 14 Days of ColdFusion 9 Caching: Day 13 - Retrieving the Server Cache Properties (November 29, 2009)
- 14 Days of ColdFusion 9 Caching: Day 12 - Removing All Items in Cache (November 28, 2009)
- 14 Days of ColdFusion 9 Caching: Day 11 - Reporting On All Items in Cache (November 27, 2009)
- 14 Days of ColdFusion 9 Caching: Day 10 - Session-Specific Caching (November 26, 2009)
- 14 Days of ColdFusion 9 Caching: Day 9 - Dependent Caching (November 25, 2009)
- 14 Days of ColdFusion 9 Caching: Day 7 - Using cacheRemove to Flush Items from Cache (November 23, 2009)
- 14 Days of ColdFusion 9 Caching: Day 6 - Using cachePut, cacheGet, and cacheGetMetadata (November 22, 2009)
- 14 Days of ColdFusion 9 Caching: Day 5 - The Difference Between Timespan and Idletime (November 21, 2009)
- 14 Days of ColdFusion 9 Caching: Day 4 - Flushing a Template From Cache (November 20, 2009)
- 14 Days of ColdFusion 9 Caching: Day 3 - Viewing Page Cache Metadata (November 19, 2009)
- 14 Days of ColdFusion 9 Caching: Day 2 - Caching a Page Fragment (November 18, 2009)
- 14 Days of ColdFusion 9 Caching: Day 1 - Caching a Full Page (November 17, 2009)