Question

I've been blind sided by the change from CF8 to 9 where write to disk caching is no longer possible without creating a custom cache or some other drastic workarounds that are not worth the effort.

At this time I've abandoned trying to convert the application to uphold, if possible, the same method of writing the contents of a cache file into a static cfm file. I'm more curious now that I've dug into it a bit deeper. I'm looking for someone that has more experience with this than myself.

What I'd like to understand or know how to do with template cache is:

  1. Be able to target a template in default or custom cache and flush it without clearing the entire cache.
  2. View or parse the contents of a particular cached template either out of curiosity or debug approach.

This was the code I was tooling with, requires CF 9.0.1 due to some cache memory functions that are not available in 9.0 due to the addition of EhCache 2.0 I believe.

<cftry>
        <cfcache action='serverCache' timeout='#CreateTimeSpan(0,0,0,10)#' stripwhitespace='true' 
                 usequerystring='true'>
            Stuff to cache.
            <br/>
            <cfoutput>#now()#</cfoutput>
            <br/>
        </cfcache>

        <!--- Get the cached contents --->
        <cfdump var="#cacheGetProperties()#">
        <cfdump var="#getAllTemplateCacheIds()#">
        <!---Locate a single cached item --->
        <cfscript>
            cacheArray = getAllTemplateCacheIds();
            WriteOutput("Before<br/>");
            for(i=1;i LTE ArrayLen(cacheArray);i=i+1)
            {
                writeOutput(cacheArray[i] & "<br/>");
                if(FindNoCase(scriptPath, cacheArray[i]))
                {
                    //expect only to find min and max one per string so no need to worry about out of bounds
                    cacheIDSubStr =  REFind("[a-fA-F\d]{32}(?=_LINE:\d*$)",cacheArray[i],1,1);
                    cacheID = Mid(cacheArray[i],CacheIDSubStr.pos[1],CacheIDSubStr.len[1]);
                   //Failure to delete cache expected fireworks
                    //WriteOutput(cacheID&"<br/>");
                    //cacheObject = CacheGet(cacheID);
                    //CacheRemove(cacheID);
                    templateCache = cacheGetSession("template");
                      //Tooling around with the exposed guts of cacheGetSession
                    WriteDump(templateCache.getKeys());
                }
            }
        </cfscript>
    <cfcatch type="Any">

        <cfscript>
            writeoutput("Error:" & cfcatch.message);
        </cfscript>

    </cfcatch>
    </cftry>

No errors should exist, but it was edited from the original to post here.

Was it helpful?

Solution

The secret is in the CF 9.0.1 function cacheGetSession(), along with the amending of the other functions to take a key as a parameter.

The following code will only work in 9.0.1.

Assuming:

<cfcache action="serverCache" timeout="#CreateTimeSpan(0,0,0,10)#" stripwhitespace="true" usequerystring="true">
Stuff to cache in the default store.
<br/>
<cfoutput>#Now()#</cfoutput>
</cfcache>

<cfcache action="serverCache" key="myCustomCache" timeout="#CreateTimeSpan(0,0,0,10)#" stripwhitespace="true" usequerystring="true">
Stuff to cache in the 'myCustomCache' store.
<cfoutput>#Now()#</cfoutput>
</cfcache>

Accessing the identifiers of the cache separately

Default (Template):

<cfdump var=#getAllTemplateCacheIds()#>

myCustomCache:

<cfdump var=#cacheGetSession('myCustomCache',true).getKeys()#>

Reading the data of the custom cache

<cfset keys = cacheGetSession('myCustomCache',true).getKeys() />

<cfcache key="myCustomCache" action="get" name="dataInCache" id="#keys[1]#">

<cfdump var=#dataInCache#>

Flushing the custom cache independently of the default

<cfset cacheRemove(keys[1],true,'myCustomCache')>

Sadly, documentation is not at its best with regards to the functionality that snuck in with 9.0.1. Luckily, the CF userbase is good at digging this stuff out, namely Rob-Brooks Bilson, who has a number of great articles on CF and caching in general.

Couple of caveats you should remember in working with the cache in this fashion:

  1. These calls tie to EhCache under the hood, which is Java, and may return NULLs if you try to access keys that don't exist. Wrap your results in IsNull() to validate.

  2. cacheGetSession() is misleading (as Rob points out on his blog), you may think you are retrieving session-related cache data as it applies to your app, but that is not the case--it is server-wide, and you will see other cache's keys in a share app environment.

So, take care to flush the appropriate data...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top