ScopeCache

Today a discussion about partial page caching came up on cf-talk. I mentioned a custom tag I had written about for CFDJ, ScopeCache. I could have sworn I blogged about it before, but since I couldn't find it in my archives, I must have forgotten.

So, with that in mind, I decided to add a few new features to it and upload it. ScopeCache allows you to easily cache partial page content. This is done by simply wrapping content:

<cf_scopecache name="stockInfo" scope="application" timeout="#30*60#">
do some slow stuff here
</cf_scopecache>

This code will cache the result of the code inside the call for thirty minutes. The older version didn't even have a timeout. (Don't ask me why, I just never added it.) You can set a timeout to a specific date and time, or by a number of seconds.

Also included are dependancies. This allows you to "chain" caches. So if cache item A has a dependancy of B, it means that when A is cleared, or removed because of a timeout, then B will be cleared as well. This process is chained so that if B had dependancies, they will be cleared as well.

The next feature I'm considering adding is a "size" to the cache. This would allow you to say "Only cache N items". Removal would be done like .Net's cache system - each item in the cache would have a "weight" and the lightest items would be removed first. (By the way, if you haven't checked it out, the built-in cache system in .Net is very sweet.)

If you have any questions, or suggestions, about this code, just let me know.

Edited Oops, a link would help, wouldn't it? You can download the code here.

Archived Comments

Comment 1 by Mike Brunt posted on 1/9/2004 at 12:06 AM

Very nice Ray as always, thanks for all the support you give to the CF and other communities.

Comment 2 by Tony posted on 1/9/2004 at 2:06 AM

Hey Ray... where can we get an updated version of this tag? Sounds great!

Comment 3 by Raymond Camden posted on 1/9/2004 at 2:10 AM

Ah geeze. A link would help, wouldn't it? I'll edit the post and add the link. Please check again in 5 minutes.

Comment 4 by Tony posted on 1/9/2004 at 8:23 PM

Thanks Ray!

Comment 5 by Fiona posted on 3/25/2004 at 9:11 PM

Hi Ray, You mentioned that you are adding a size to your caching system. At the moment is there a limit that you would suggest for what you put in the cache. Should this tag be limited to just small items or could you use it for a large number of items of text and graphics etc?

Comment 6 by Raymond Camden posted on 3/29/2004 at 6:53 PM

Well, it is hard to give a hard and fast rule. In general, here are some thoughts:

You can cache to the session, however, if you site has a large # of users and large # of dynamic pages, you could quickly generate a HEAVY load in RAM. In that case, I wouldn't use scope cache, or only cache some items.

Note your average RAM usage BEFORE using ScopeCache. Then note it afterwards. If you see an impact, a bad impact, scale back your use of scopecache.

Hope this helps a tiny bit.

Comment 7 by hungry posted on 3/22/2007 at 12:41 AM

i just started testing your scopecache tag in my app and REALLY like it...i was thinking it would be good to have a max number of items to try and limit the amount of ram (the app is going to be in a shared server environment) and just re-found your post mentioning the same...i think i might take a crack at it...just curiuos if you have any thoughts on the structure of such a function or if perhaps looking into using some of the ideas in softcache.cfc to do dynamic memory management (though i can't seem to find a working link for softcache.cfc).

Comment 8 by Raymond Camden posted on 3/22/2007 at 4:35 AM

If you wanted to use a FIFO (first in/first out), then you would need to store the order of the items as they are inserted... or make scopeCache store when the item was created. I think it might do that now. Anyway that would let you remove the oldest when you have too many items. Just one idea.

Comment 9 by KANDA posted on 4/18/2012 at 4:23 PM

Hi, sorry for very stupid question.
If I make a new structure inside this scopecache.

<cf_scopecache name="test1" scope="application" timeout="#30*60#">
<cfset application.test = structNew()>
<cfset a = 1>
<cfset b = 2> etc....
</cf_scopecache>

then... how do I call this structure ?
why can't I call it directly using #application.test#

Comment 10 by Raymond Camden posted on 4/18/2012 at 5:32 PM

That's not really a good use of scopecache. If you want to set application variables and set them once, I'd do one of:

1) In Application.cfm, have something like:
<cfif not isDefined("application.test")>
<cfset application.test = 1>
</cfif>

2) In Application.cfc, use onApplicationStart.

Comment 11 by KANDA posted on 4/18/2012 at 6:37 PM

THANKS a LOT for replying.