Clearing individual files/folders from ColdFusion template's cache

This post is more than 2 years old.

A little bit ago I sat in the session "End to End Performance Tuning", which was a great session. One of the tips mentioned was the use of trusted cache on production systems. One of the problems with this though is that you need to clear the template cache for each and every little change. What folks tend to forget is that the Admin API has support to clear individual files. I blogged about this last year (at CFUN I think) and wrote up a quick little script to show an example of it. This came up again during the session so I thought I'd work a bit on the script and make it a bit more presentable in the ColdFusion Admin.

Take the script attached to this template and put it in your ColdFusion Administrator folder. Put it in a subfolder named anything you want, I used cacheclearer. Then edit custommenu.xml to add a link:

<menuitem href="cacheclearer/" target="content">Cache Clearer</menuitem>

Then hit up your Admin and you can run the utility. You can enter a CFM/CFC to clear or a folder and all CFM/CFCs under it will be clearer.

Download attached file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, 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 https://www.raymondcamden.com

Archived Comments

Comment 1 by James Moberg posted on 6/20/2008 at 2:07 AM

Please identify the ColdFusion requirements as necessary.

This appears to be a CFMX 8-Only method. Can this be modified to work with CFMX 6 or 7? If so, is there any easy way to integrate it?

Comment 2 by Raymond Camden posted on 6/20/2008 at 2:09 AM

Right, this is CF8 only. As far as I know there is no way to do this with CF7.

Comment 3 by Casey posted on 6/20/2008 at 5:42 AM

Have there been any tests to determine the performance difference with trusted cache enabled/disabled?
By the way, the image does not work.

Comment 4 by Gary Fenton posted on 6/20/2008 at 1:00 PM

Thanks Ray, this is a nice solution to the problem. I've been using trusted cache for a couple of years now but found it annoying to clear it every time we have the most minor of code changes. An unsung feature of trusted cache is if you have code changes in multiple files across a few directories and you need to make all changes live at exactly the same time (because of file dependancies) then it's impossible without the use of trusted cache.

For the past month I've turned off trusted cache and haven't noticed any performance changes. I suspect it's only noticable if your server is running close to its limit, then it may shave a few milliseconds off each request - which adds up if you have lots of requests per second.

Comment 5 by Raymond Camden posted on 6/20/2008 at 4:44 PM

Casey - Adobe 100% recommends turning this feature on, and I know I've definitely seen improvements when doing it. Fixed the image.

Comment 6 by James Moberg posted on 6/20/2008 at 6:09 PM

Ray,

Is it possible to add a function to "onrequestend.cfm" to take a parameter (URL variable, IP address, browser agent, time window) and then determine which templates were recently used to generate the current page and clear them from the trusted cache?

If it is possible, would this method be advisable?

Comment 7 by Scott P posted on 6/20/2008 at 11:51 PM

Adding a pointer to http://templatelistener.ria... for those that can use event gateways.

Comment 8 by Charlie Arehart posted on 6/21/2008 at 6:35 PM

@Casey, there's clearly a performance benefit in that using trusted cache saves an i/o if a template can be found in the cache. CF then doesn't look to disk to test if the file has changed. When you know it will not, it's a good feature.

Of course, if the template cache is too small, then the value of having trusted cache enabled may be diminished, as the cache is thrashed removing requests and recompiling them.

Indeed, when Gary says he experienced no difference by enabling it, I'd wonder if that was the issue (unless it's simply that he didn't have enough requests on his server to notice any benefit from the i/o saved, which may be small per request but would increase in total value with more requests.)

@James, I don't understand what you're asking. What connection would you expect "recent use" to have on whether templates should be cleared from the cache? All that matters is whether the template has been *edited*, which is why tools that use the filewatcher gateway (as pointed to by Scott P) would be the way to go.

Comment 9 by James Moberg posted on 6/22/2008 at 4:16 AM

@Charlie, by recently used I meant all templates that were included or accessed in the page request.

For example if a simple template was updated, it would be nice to be able to pass a parameter or something and have an "onrequestend" function determine which templates were used in the generation of the page and delete them from the trusted cache. This would save a trip to the CFAdmin and requirement to find the templates that need to be deleted.

I looked at the solution offered by Scott P and the project states that ColdFusion 8 is a requirement... although I downloaded the project and the readme.txt has installation instructions for CFMX 6 & 7 but doesn't mention 8. I'm not sure if there a legitimate 8 requirement or whether it will it also work with 6/7.

I guess what frustrates me the most is reading an article and not knowing whether or not it will work on CFMX 7 because the writer takes it for granted that everyone is using 8. This has happened a couple of times in other forums and I find myself wasting time attempting to determine why something isn't working only to find that the developer uses new CF-8 only syntax or requires new hidden features not available in 7. I sometimes spend even more time trying to figure out a work-around to make it work. Sometimes I'm successful and other times I am not.

Comment 10 by Scott P posted on 6/22/2008 at 6:56 AM

@James - clearing the cache will work on 7. The Difference between 7 & 8 is that in 8 you can clear a single or list of files from the cache instead of the whole cache.

If you are on 7 then you will be stuck with clearing the entire cache - the same as clicking on the clear template cache button in the admin. You could set the code to do that part for you.

Comment 11 by Raymond Camden posted on 6/22/2008 at 3:56 PM

@James - I guess that's my fault - not making it clear it was CF8 only. Although in my defense, the article I linked to clearly said it was cf8 only. :)

Comment 12 by James Moberg posted on 6/22/2008 at 5:44 PM

@Scott P - Thanks for the clarification. I currently have a method exposed in CFMX7 to clear the entire cache and was hoping to find a better way of doing it without having to resort to an upgrade.

@Ray - I read the linked article and thought that all the magic was in the Admin API and set of CFCs that may have also been possible to work with CFMX7. I didn't know if this was just a couple of new scripts that were added or if is an internal java update that allows it to do it. Is it possible that these CF8 CFCs would also work in CFMX7 but are available only in CF8?

Some organizations I work with remain on CF5 and CFMX6 due to budget constraints and the fact that "it's not broken" and "doesn't need updating." This requires me to integrate scripts that work across as many versions as possible to ensure that I can upsell new features to them as no new CF8 features that I've mentioned have enticed them to upgrade yet.

Comment 13 by Raymond Camden posted on 6/22/2008 at 5:53 PM

Ah, well, in that old article I do say: "Along with cool tags and functions added in ColdFusion 8, there have also been some neat updates to the Admin API." That's pretty clear I'd say. ;) But I shoulda been clear in this post as well. As far as I know you can't use the CF8 cfcs in CF7.

Comment 14 by James Moberg posted on 6/22/2008 at 6:14 PM

When I initially went through the new tag and function list, some CF8 features seemed like graduated CFTags and functions (that were already available) or internal wrappers for existing javascript libraries like FCKEditor and Ext (which are things I've been doing without the need for CF8.)

Don't get me wrong, I'm using CF8 too and I know that the upgrade is a lot more than just the inclusion of already existing third-party tags and libraries. I've been following many of the book, articles and blogs since CF2/3 and now more than ever I have to filter what I'm reading to determine which version (and potential client) I can actually apply these new tips too. I really don't recall having to do before as most tips seemed to be CFML-specific and not version or vendor specific.

Comment 15 by Charlie Arehart posted on 6/23/2008 at 6:50 PM

James, if you need more ammunition for appreciation of what else has changed in CF8, check out my CF8 Hidden Gems presentation: http://carehart.org/present...

There are several dozen small (and some important) changes beyond the big things you hear talked about all the time.

Comment 16 by Barbara ONeal posted on 7/13/2008 at 12:57 AM

Ray,

I downloaded and placed the code on my server. I just ran inot an issue where is does't actually remove any of the class files. If i put in an individual file it says that it is going to clear it. But it still exist in the directory. So i thought hmmm i will just do the entire direcotry. Well that didn't work either. For example: C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfclasses then select clear file/folder It doesn't throw an error it just doesn't do anything.

Ideas?

Comment 17 by Raymond Camden posted on 7/14/2008 at 6:10 AM

I think you misunderstand what this tool does. It does not delete the class files. When you turn on trusted cache, CF stops checking a CFM file to see if it has been updated and needs to be compiled again.

This utility doesn't clear the class files. Instead, it tells CF, "Hey, for these files, you need to check them on next execution to see if they need rebuilding."

Comment 18 by Yaron Kohn posted on 11/11/2008 at 12:48 AM

Love this tool...thanks Ray...
I would suggest adding this line of code to the bottom of the page....It sets the "Last Page Visited" cookie like all the other admin pages.

<cfset cookie.CFADMIN_LASTPAGE_ADMIN = cgi.SCRIPT_NAME>

Comment 19 by Raymond Camden posted on 11/11/2008 at 12:56 AM

Yaron, thank you for this tip! This application here doesn't have a proper RIAForge project I think so folks tend to forget about it, but I may correct that. I'm also going to add this to SpoolMail. I also have a Verity tester CF Admin plugin.

Comment 20 by Charlie Arehart posted on 1/24/2009 at 8:52 AM

Hey Ray, you mentioned in a comment here a couple of months ago that you might create a riaforge project from the cache clear/filewatcher combo. Did you ever get to? I didn't want to try to search fearing I may not use the right terms and figured folks would love to hear if you did.

Also, I would think this is a feature set that should be built into CF (or at least an admin interface to point to templates to clear manually). Have you made either suggestion as a feature request? It may be considered for Centaur, which would be awesome.

Comment 21 by Raymond Camden posted on 1/24/2009 at 8:41 PM

Nope, I did not make a RIAForge project out of it. Think its worth it?

I did file an enhancement request for this with Adobe (like 2 mins ago).

Comment 22 by Charlie Arehart posted on 1/24/2009 at 9:05 PM

Well, I definitely think it's worth a project. Otherwise only those who find this entry and the other will know of it. It's just an awesome solution to an important problem (for those who recognize it, at least.)

Really great to hear you've requested it of Adobe. Of course, if it gets in then one could argue that the need of a project is diminished, but since 9 is still in the future, and even then many may remain on 8 who would benefit, it seems worth it, sure.

Comment 23 by Raymond Camden posted on 1/24/2009 at 10:39 PM

Alright, I'll look into it. I do see some benefit of just making it easier to find. Although I hope people are also using my CF Admin Extensions Guide: http://www.coldfusionjedi.c...

Comment 24 by Eric P posted on 3/24/2009 at 8:57 PM

Hi, thanks a lot for this info. I've modified the extension a bit (to accept a list of files) and it's working great.

Is anyone aware of a way to check the state of the trusted cache toggle? I thought it'd be cool to deactivate the 'cache clearer' form when the trusted cache is toggled off.

Thanks again,
Eric P

Comment 25 by Raymond Camden posted on 3/25/2009 at 1:29 AM

I think supporting a list of files would be nice. I normally just enter a directory, but a list would be cool. Send me your mod.

You can tell if the trusted cache is on with the Admin API. It may be cool to add that to my tool as a flag. "Are you sure you want to clear cache? Trusted cache isn't even turned on, Bozo."

Shall I add that?

Comment 26 by Eric P posted on 3/31/2009 at 4:40 AM

Sent via your Contact Me form.

By the way where is the Admin API documented?

Thanks,
Eric P

Ps. the lucky captcha I got to type was 'die'. (*shiver*)

Comment 27 by Raymond Camden posted on 3/31/2009 at 4:49 AM

In the developer's guide I believe - although not every single method. If you view the CFC in your browser, you can see a list of methods. (Don't forget you can open a CFC in your browser to see automatically generated documentation.)

Comment 28 by Raymond Camden posted on 4/2/2009 at 10:33 PM

Live: http://cacheclearer.riaforg...

I'll update the link in my guide soon.

Comment 29 by Raymond Camden posted on 4/2/2009 at 10:35 PM
Comment 30 by Vad posted on 9/16/2009 at 3:10 PM

I am using CFMX and there is an issue (variable "yy" is undefined in "yyfiling")thats a show stopper , on the production.

I am promoting corrections to it but they do not seem to show up on the server.

I want to resort to clearing the Server cache so that my promoted code can take effect.

The CFAdmin production template was cached (Trusted Cache turned on). so wanna turn it off Trusted Cache and clear template cache.

So was wondering it will effect the main site?
ANY PRECAUTIONS, as this is for a production server?

Thanks
-Vad

Comment 31 by Raymond Camden posted on 9/16/2009 at 3:45 PM

Does it really say yy in yyfling? That doesn't sound like my code. As it stands, I'm not sure if MX had the Admin API setting for this.

Comment 32 by Vad posted on 9/16/2009 at 3:59 PM

I am sorry to be out of context here, but i want to ask a general question.
Is what are the precautions to be mad if at all the server cache is to be cleared on CFMX admin?esp a production server.

I have the detailed issue i faced over here.
http://stackoverflow.com/qu...

Comment 33 by Raymond Camden posted on 9/16/2009 at 4:01 PM

If by server cache you mean the Server scope, then none. The cache clearing here just refers to the CFM cache.

Comment 34 by Vad posted on 9/16/2009 at 4:53 PM

Thanks a lot.

I meant in CFAdmin/cache/templated cache

I think the template was cached (Trusted Cache turned on),
So I want to turn off Trusted Cache and clear template cache.

As it is Live, wanted to know if any precautions?

Comment 35 by Raymond Camden posted on 9/16/2009 at 5:18 PM

The only real concern is that your next few hits will be a bit slow since has to re-cache stuff.

Comment 36 by Vad posted on 9/16/2009 at 5:33 PM

In this case....
The form fields were in saved session variables and then stored in a SAVED form.
My error was in opening the saved form.
I corrected it and pushed the code.
So was hitting on the last option as to turn off Trusted Cache and clear template cache.

Now would the SAVED forms with the old session-id be gone?

Comment 37 by Matt posted on 5/18/2011 at 5:09 AM

Thanks for this Ray.

I have my dev and live sites on the same box and would love to have the best of both worlds... trusted caching for the live site but non-cached templates on the dev. Do you have any ideas on how I could accomplish this without going to a dedicated dev server?

Comment 38 by Yaron Kohn posted on 5/18/2011 at 5:21 AM

You can create an additional CF instance for dev and leave template caching off. (This only applies to a multi CF installation)

Comment 39 by Raymond Camden posted on 5/18/2011 at 6:20 AM

@Matt: Yaron answered, but let me just add - I strongly urge you not to run dev and production on the same physical box. Remember - ColdFusion is free for your dev server.

Comment 40 by Hal Dunithan posted on 12/17/2011 at 2:09 AM

Hey Ray,

How can I make the CacheClearer custom menu option available in CFAdmin for my team without giving them full admin access? I've allowed them Console & API with the roles "Debugging & Logging > Logging" and "Server Settings > Settings Summary" but the custom menu generates a forbidden message when they attempt to run it.

Comment 41 by Raymond Camden posted on 12/17/2011 at 6:50 AM

Unfortunately, you can't. I'd just use the code _outside_ of the CF Administrator.

Comment 42 by Matt posted on 9/17/2012 at 5:00 AM

I'm trying to use it outside of my Administrator and it doesn't seem to be working.

I'd love to be able to add this to the root of my test sites so developers can flush when they need to without having Admin access.

Thanks!

Comment 43 by Raymond Camden posted on 9/17/2012 at 10:44 PM

Are the calls to the Admin API not working?