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.

<!--- If the URL parameter 'flush' is defined, flush the cache. --->
<cfif isDefined("URL.flush")>
    <cfset cacheRemove("wt-7-cache")>

<!--- 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 />">

    <cfset cachePut("wt-7-cache", cachedData, CreateTimeSpan(0,0,0,15))>

<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.

Aaron West's Gravatar
About this post:

This entry was posted by Aaron West on November 23, 2009 at 8:00 AM. It was filed in the following categories: ColdFusion. It has been viewed 7112 times and has 6 comments.

13 related blog entries

6 Responses to 14 Days of ColdFusion 9 Caching: Day 7 - Using cacheRemove to Flush Items from Cache

  1. Roman

    Is this supposed to work in CF9 or only in CF9.0.1? I have it working correctly on my Dev server which is running 9.0.1, but it's not working on stage or prod, which are running 9.0. Not sure if that's the reason or if it could be something else.

  2. @Roman - The cacheRemove() function was introduced with 9.0 and should work on 9.0 or 9.0.1. When you say it isn't working, what do you mean? You are still seeing the item in the cache? Can you share a code example that I can try on my servers?

  3. Roman

    Humm, what I'm doing is something simple.

    Here is the basic code flow...

    Put some HTML into cache
    <cfset cachePut("htmlcache_#CGI.SCRIPT_NAME#_#CGI.QUERY_STRING#",cachedHTML,createTimeSpan(0,0,0,30))>

    Check what's in the cache:
    <cfset arrayCacheIDs = CacheGetAllIds()>
    <cfset listCacheIDs = "">
    <cfdump var="#arrayCacheIDs#" label="before">

    Figure out IDs to remove and remove them:
    <cfloop from="1" to="#ArrayLen(arrayCacheIDs)#" index="i">
    <cfif FindNoCase("leaderboards", arrayCacheIDs[i])>
    <cfif ListLen(listCacheIDs) GT 0>
    <cfset listCacheIDs = listCacheIDs & ",">
    <cfset listCacheIDs = listCacheIDs & arrayCacheIDs[i]>

    <cfif ListLen(listCacheIDs) GT 0>
    Removing matched CacheIDs
    <cfset CacheRemove(listCacheIDs, "false")>

    Check what's in the cache now.
    <cfset arrayCacheIDs = CacheGetAllIds()>
    <cfdump var="#arrayCacheIDs#" label="after">

  4. I think the problem is with your cache loop where you are looking for specific cached items and only removing certain items. I recommend verifying what is in cache and ensuring there's an item in cache that you expect your loop to find.

    Check out the following post in this series where I talk about reporting on all items in the cache. And remember, there are two different types of cache, object cache and template cache.

  5. Roman

    I don't think your link to the post came through. But I am doing the dump of everything in cache before and after. On dev, there is just one item in cache when I add it, and nothing after I remove it. On Stage and prod, it's there after I add it and after I remove it.

  6. Ack, you're exactly right, I forgot the paste the link. My apologies. Here it is: