Search code examples
coldfusioncfcache

Flushing individual cached page fragments


Im not sure if this is possible..

I am dynamically generating table rows, and want to cache each row as a page fragement.. such as

<cfloop index="i" from="1" to="10">
    <cfcache id="tableRow_#i#">
        <tr><td>..some stuff..</td></tr>
    </cfcache>
</cfloop>

Then in other code, in a completely different part of the Applicaiton, I want to be able to flush individual fragments.. for example, if I want to flush 'tableRow_2'..

<cfcache action="flush" id="tableRow_3">

Can anyone tell me if this type of granularity is possible, and if so what the best approach would be.

The closest I have been able to find is <cflush expireURL="..">, but this flushes all caches in the page.. where I need to be able to flush individual caches in a page.

Many thanks in advance!

Jason


Solution

  • One way you could handle this is via an application-scope cache pool. For example:

    <cfif not IsDefined("application.cachePool")>
      <cfset application.cachePool = {}>
    </cfif>
    
    <cfloop index="i" from="1" to="10">
        <!---<cfcache id="tableRow_#i#">--->
        <cfif not StructKeyExists(application.cachePool, "tableRow_#i#")>
            <cfsavecontent variable="cacheTmp"><tr><td>..some stuff..</td></tr></cfsavecontent>
            <cfset application.cachePool["tableRow_#i#"] = cacheTmp>
        </cfif>
        #application.cachePool["tableRow_#i#"]#
        <!---</cfcache>--->
    </cfloop>
    

    Then later on, elsewhere in the app, you can use StructDelete:

    StructDelete(application.cachePool, "tableRow_3")