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:

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

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA