For today's post-holiday caching entry I want to look at code that shows how to report information (metadata) on all items stored in cache. This includes both template cache and object cache. And since it's a holiday and I have some turkey and dressing to get to, let's jump right in. Here's the example code:

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

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

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

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

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

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

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

To introspect the metadata for all items in the cache I first load it up with items. I start with the object cache and insert a simple string followed by an array. 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.

The screenshot shows the metadata for the two items stored in object cache, the two cached page fragments, and the one uncached date/time string. Metadata for the two cached page fragments shows last. Using code like this to view everything stored in cache is nice but I'm not sure how practical it is. In an enterprise application where you'd have hundreds of items stored in object cache and potentially hundreds of cached pages, code to view everything would be really heavy and slow. You'd probably write caching utilities that inspected certain items in cache versus everything. However, it would be cool if the ColdFusion Administrator had an interface that would let you look at certain parts of cache. I suppose you could build this kind of interface on your own as a custom page or administrator extension.

No matter how you look at it, having the ability to look inside the cache and make decisions about your application is an important aspect of good Web development.

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 27, 2009 at 11:42 AM. It was filed in the following categories: ColdFusion. It has been viewed 7499 times and has 0 comments.

13 related blog entries

0 Responses to 14 Days of ColdFusion 9 Caching: Day 11 - Reporting On All Items in Cache