Nov
28

Yesterday I showed examples loading up template and object cache with sample data and viewing the sample data with a series of <cfdump> statements. Today, I want to take this concept a bit further and show you how to remove all items stored in both template and object cache.

Let's start with code that loads up the cache. This code is exactly the same as yesterday's with the exception of the names used for the cache keys.

<!--- Put a string in the Object cache. --->
<cfset cachePut("wt-12-string-cache", "This will become a cached string.", CreateTimeSpan(0,0,2,0))>

<!--- Put an array in the Object cache. --->
<cfset cachedArray = ["item 1", "item 2", "item 3", "item 4"]>
<cfset cachePut("wt-12-array-cache", cachedArray, CreateTimeSpan(0,0,2,0))>

<!--- Display information about all items in the Object cache. --->
<h4>Object cache</h4>
<cfloop index="cache" array="#cacheGetAllIds()#">
    <cfdump var="#cacheGetMetadata(cache)#" label="Cache Metadata for #cache#">
</cfloop>

<!--- Put a page fragment in the Template cache. --->
<h4>Template cache</h4>
<cfcache action="cache" timespan="#CreateTimeSpan(0,0,2,0)#">
    <cfoutput>This date/time IS cached: #Now()#<br /></cfoutput>
</cfcache>

<!--- Put a second page fragment in the Template cache. --->
<cfcache action="cache" timespan="#CreateTimeSpan(0,0,2,0)#">
    <cfoutput>This date/time IS ALSO cached: #Now()#<br /></cfoutput>
</cfcache>

<cfoutput>This date/time is not cached: #Now()#</cfoutput>

<!--- Display information about all items in the Template cache. --->
<cfdump var="#GetAllTemplateCacheIDs()#">

First, I insert a simple string and array into the object cache. To view all items stored in object cache I use the new cacheGetAllIDs() function - which returns an array of cache IDs (aka keys) - in a <cfloop>. In the body of the loop the cache key (index of the loop) can be passed to the cacheGetMetadata() function. As the loop progresses the metadata for each cache item is dumped to the browser.

For template cache, I cache two page fragments that each display the current date/time in a simple sentence. And finally, I display what little metadata is available for template cache using the undocumented GetAllTemplateCacheIDs() function.

All the code used to load up the cache and report on what's there results in browser output that looks like this.

With a template written to populate and report on the cache we need to write a second template to remove all information stored in cache. The following code should do the trick.

<!--- Remove the Object cache. --->
<cfloop index="cache" array="#cacheGetAllIds()#">
    <cfset cacheRemove(cache)>
</cfloop>

<!--- Remove the Template cache. --->
<cfcache action="flush">

Done... now dumping any available Object and Template caches.

<!--- Display information about all items in the Object cache. --->
<h4>Object cache</h4>
<cfloop index="cache" array="#cacheGetAllIds()#">
    <cfdump var="#cacheGetMetadata(cache)#" label="Cache Metadata for #cache#">
</cfloop>

<!--- Display information about all items in the Template cache. --->
<h4>Template cache</h4>
<cfdump var="#GetAllTemplateCacheIDs()#">

I start with the object cache and use a <cfloop> coupled with the cacheGetAllIDs() function. Inside the body of the loop I pass the index of the loop - the cache key - to the cacheRemove() function. This loop will run just as many times as there are items in the object cache. When finished, there will be nothing stored in the object cache.

Clearing the template cache is a bit easier. All that's needed here is a simple <cfcache> tag with the action attribute set to flush. Every page or page fragment stored in template cache (for the current Application) will be removed.

To prove everything has been removed from cache I reuse the code from the first example above which iterates the object cache displaying anything available. The template cache is displayed using the undocumented GetAllTemplateCacheIDs() function.

In order to see this in action you'll need two templates each with one of the example code blocks from above. You can create these templates or download them using the link at the bottom of this post. If you're using the downloaded code, the first template will be called wt-12.cfm and the second wt-12a.cfm. Run the first template to load up the cache and see browser output similar to the screenshot above. Within two minutes, run the second template to remove everything from cache. The second template will have browser output that looks like the following.

If everything works as designed all items in cache will be removed. If you want to triple check the code you can rerun the first template and pay special attention to the createdtime date/time shown in the two object cache dumps. The time should be the current server time. Then, rerun the second template to clear the cache. Each time you run the templates as a series new items will be cached and then subsequently removed. That's it for today's post, come back tomorrow where I'll show you how to retrieve the overall server cache properties.

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 28, 2009 at 8:55 PM. It was filed in the following categories: ColdFusion. It has been viewed 10311 times and has 2 comments.

13 related blog entries

2 Responses to 14 Days of ColdFusion 9 Caching: Day 12 - Removing All Items in Cache

  1. The only way I found for clearing all cached items in cfscript is that :

    cacheRemove(arrayToList(cacheGetAllIds()), false);

    In according with documentation of cacheRemove() :
    - ids : A comma delimited string of IDs of the cached objects to remove.
    - throwOnError :A Boolean value specifying whether to throw an exception if any ID does not specify a cached element.


    No better way ?

  2. Ferg

    There is a surprising lack of information out there about using the object cache functions in a load balanced environment. Has anyone out there worked with cacheget, cacheput and cacheremove in a load balanced env? I'm really interested in hearing how you've made it work...