Safer and More Dynamic ColdFusion Page Caching with cf_MrECache

I looked at quite a while back and I liked the idea, but it lacked a little bit of flexibility that I wanted in pursuit of my caching strategy.

I recently covered MRECache and now I'd like to briefly cover a custom tag that is built around it.

At its heart, cf_MrECache just wraps up a block of text (HTML or whatever) that you want to cache and caches it just like cfcache, except that is uses MRECache under the hood. Of course, if that were all that it did then it would hardly be worth making in the first place.

Before I cover the advantages of cf_MrECache over cf_cache, I'd like to go over the drawbacks.

  1. cf_MrECache doesn't know what line number it was called from, so if you have more than one on the same page then you will have to add some data to the "data" attribute to differentiate them.
  2. cf_MrECache uses a file that has mixed case, so you will have to match that case if you aren't using Windows. I like mixed case, sorry.

Now, to the advantages.

1. Clearing All File Caches

All cf_MrECache caches are stored in a "files" id. So, you can use the following code to remove all instances of cf_MrECache from the cache:

oMRECache = new com.sebtools.MRECache(id="files");
oMRECache.clearCaches();

2. Extra data:

The data attribute can allow for caching the page multiple times depending on different data.

For example, maybe you have a finite list of templates for your site, saved in session scope.

If so, you could call the tag with

<CF_MrECache data="#Session.style#">

Then you would get a different cache for that page for each different value of Session.style.

3. Uses CGI.PATH_INFO:

If you have useQueryString="true" then CGI.PATH_INFO will be included in the cache id.

4. Targetted Clearing

Because of how MREcache handles clearing caches, you have a lot of flexibility in how you clear the cache.

oMRECache = new com.sebtools.MRECache(id="files");

If you want to clear all caches for "/example.cfm"

oMRECache.clearCaches("/example.cfm");

You could also clear:

oMRECache.clearCaches("/example.cfm?id=");

for any path starting with that.

Or, you could use a folder. If you have an "example" folder then you could

oMRECache.clearCaches("/example/");

to clear every page within it from the cache.

5. Targetted caching

If you use useQueryString="true" then you can use the "regex" attribute to define the acceptable format for the string after the file name.

If the format doesn't match the regex, then only the file path will be used as the cache id.

For example, you could decide to only use the query string if it consists only of an integer id.

<cf_MrECache regex="^\?id=\d+$">

6. DDOS Mitigation

Efforts are put in to make DDOS attacks less impactful for pages using .

By its nature, caching reduces the load on a server, which helps protect from DDOS. However, if every URL produces a new cache and a DDOS attack spams with tons of URLS then you could build up an excess of data in RAM.

For legitimate pages, cf_MrECache won't help with this.

However, your page could have lots of values for which it returns the same result. For example, if you have a product.cfm with 100 products. product.cfm?id=1 through prouct.cfm?id=100 would all return different product pages. product.cfm?id=101 and above, however, would all return the same content indicating that no product was found.

cf_MrECache will store a separate key for each page, but will only store the full text of the page once.

This will save space in the cache.

Examples

Here are some examples of using cf_MrECache:

<cf_MrECache>
<html>
<head>
   <title>My Page</title>
</head>
<body>
   <p>Page contents</p>
</body>
</html>
</cf_MrECache>

Or how about this?

<html>
<head>
   <title>My Page</title>
</head>
<body>
<cf_MrECache>
   <p>Page contents</p>
</cf_MrECache>
</body>
</html>

OR

<cf_MrECache useQueryString="true" regex="^\?id=\d+$">
<html>
<head>
   <title>My Page</title>
</head>
<body>
   <p>Page contents</p>
</body>
</html>
</cf_MrECache>

<html>
<head>
   <title>My Page</title>
</head>
<body>
<cf_MrECache useQueryString="true" regex="^\?id=\d+$" data="first">
   <p>Page contents</p>
</cf_MrECache>
<cf_MrECache useQueryString="true" regex="^\?id=\d+$" data="second">
   <p>Page contents Part II</p>
</cf_MrECache>
</body>
</html>

Enjoy!

MRECache is part of my com.sebtools package. It is a free ColdFusion package and you can read more about it in my com.sebtools blog series. Check it out!

If you need any help with com.sebtools, or ColdFusion in general, let me know.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.