ColdFusion Muse

Illustrating Invalid Characters in Structure Keys

Mark Kruger November 14, 2005 6:26 PM Coldfusion Tips and Techniques Comments (0)

In a previous blog entry titled IsDefined() Vs. StructKeyExists() - The Nuances of CFMX Structures, I illustrated the point that non-standard variable names like "33foo" can be used as keys to a structure, even though setting them directly causes the dreaded "invalid variable name" error. You may not know it, but Ray Camden's popular cfc blog software exploits this feature using his "scope cache" tag to ensure unique structure keys in the cache.

When Ray initializes the cache for blog CFC he does it in the application scope using the CGI.Query_string as the key name for the application scope. Here's the snippet from the index page.

<cfset cachename = cgi.query_string>
<cfset disabled = false>
<!--- disable cache for entry or search --->
<cfif listFindNoCase("search,entry",url.mode) or isUserInRole("admin")>
   <cfset disabled = true>

<cfmodule template="tags/scopecache.cfm"
This is a nice quick trick that keeps him from having to save the query string (which in this case represents some unique view of a page, article or category) and compare it with data in the cache. But consider that the query string will always have invalid characters in it. For example, the query_string for the previoius article is:
The equals sign, ampersand and dash are all invalid characters to be contained in a variable, but because Ray only accesses them as keys to a structure (as in application["mode=entry&entry=37A3846F-0041-B76F-3DFA8B2A64CD9BEB"]) the error is never thrown and the system quite happily caches the data under these unique keys.

Incidentally, scopecache is an outstanding tag - simple and elegant. It's the grease that makes blog cfc so fast, and hence so ubiquitous among us Coldfusion types. Ray uses scope cache to build a self-tuning site that remains in memory and serves up lickety split. Good stuff!

  • Share: