Building on yesterday's example that used cacheGet() and cachePut(), today we'll look at removing items from cache. If you haven't read yesterday's post yet I encourage you to as it includes definitions for ColdFusion 9 caching terminology mentioned in this post.
Right off the bat I need to mention how I wish there was verb consistency between the <cfcache> tag and the cacheRemove() function. When you want to remove an item from cache using the <cfcache> tag, you use the action "flush." But when you want to remove an item from cache with a function you use cacheRemove(). If I had to pick which verb was more indicative of the action taken it'd be flush.
The code for today is nearly the same as yesterday with two differences. Let's take a look.
<cfif isDefined("URL.flush")>
<cfset cacheRemove("wt-7-cache")>
</cfif>
<!--- Attempt to retrieve the getOrders query from cache. Also retrieve the cache
metadata at the same time. --->
<cfset cachedData = cacheGet("wt-7-cache")>
<!--- If the data is not cached, create it and do a cache put. --->
<cfif isNull(cachedData)>
Cache doesn't exist, so create it.<br />
<cfset sleep(1000)>
<cfset cachedData = "This date/time IS cached: #Now()#<br />">
<cfoutput>#cachedData#</cfoutput>
<cfset cachePut("wt-7-cache", cachedData, CreateTimeSpan(0,0,0,15))>
</cfif>
<cfoutput>This date/time is not cached: #Now()#</cfoutput>
<h4>Cached data</h4>
<cfdump var="#cachedData#">
<h4>Cached metadata</h4>
<cfdump var="#cacheGetMetadata('wt-7-cache')#">
<br />
<a href="wt-7.cfm?flush=1">Flush Cache</a>
The first difference is the conditional logic that checks for the existence of the URL.flush parameter. If the parameter is passed on the URL we execute the cacheRemove() function passing it a key name of "wt-7-cache." Second, below the cache metadata dump is a hyperlink that runs the template again passing flush=1 in the URL.
The first time you run the template the browser output will look something like this.

There's really nothing different here from yesterday's example. The overall cache_hitcount value increases by one and the hitcount value for this specific cache key, wt-7-cache, is set to zero.
If you run the template a second time before the timespan of 15 seconds expires you'll see output that looks like this.

Some of the output isn't displayed this time since the cache already exists. The overall cache_hitcount value increases by one as does this specific caches hitcount. Also, the lasthit value now has a date/time.
If you press the Flush Cache link before the timespan of 15 seconds expires, the cacheRemove() function at the top of the page will run and the browser output will change to the following.

We see the "Cache doesn't exist, so create it." message reappear and we see all the date/times reset including the createdtime in the cache metadata dump. The hitcount for this cache key is also reset to zero due to the cacheRemove() and subsequent cachePut() function being called.
Finally, to bring this example full circle, refresh the page (without flush=1 in the browser address bar) in order to see the following output.

This output looks almost exactly the same as the second screenshot above. The "wt-7-cache" cache key exists so the item is retrieved from cache. The cache_hitcount and hitcount values both increment by one and the lasthit date/time is now populated. This example was admittedly very elementary but it still illustrates the full lifecycle of an item stored in object cache. Come back tomorrow when I'll post a more complicated example that shows the difference between cache hits and cache misses. It's great stuff!
Click here to download the code mentioned in this post.














There are no comments for this entry.
[Add Comment] [Subscribe to Comments]