Force ColdFusion Server To Recompile A ColdFusion Template
Posted May 2, 2007 at 7:04 PM by Ben Nadel
Have you ever made changes to a ColdFusion page and uploaded it only to be disappointed that your changes have not taken place. For some reason, sometimes ColdFusion does not acknowledge that the file has changed. There are probably caching settings that you can set so that this does not happen... but I don't know what those are and I'm rarely in the ColdFusion admin.
So, how do I deal with this? I put a few dozen line breaks at the end of the file and re-upload. The line breaks do not change the file in any functional way (and if they do, put them inside of ColdFusion comment tags), but this will create enough of a file size change for ColdFusion to finally take notice of the update. Once noticed, ColdFusion will recompile and serve you the new page. At this point, you can remove the spaces and re-upload (and who cares how long it take ColdFusion to recompile that version - no functional change).
This is a bit of hack, but if you didn't know this was an option, it might save you a few head-banging sessions. And, I'm not talking about needing this for every file; if you need to do this all the time, looking into other solutions. This is for that very rare situation where ColdFusion just wants to ignore your changes.
What Other People Are Searching For
- Wanted: Full-Time ColdFusion Developer at Intoria Internet Architects
- Cold Fusion Senior Developer at Edge Information Management
- Back-End Web Developer-Information Technologist at Michigan State University
- ColdFusion Developer at Nonfat Media
- Mid-to-Senior Level Web Application Developer at SiteVision, Inc.
Another thing that usually works is do a touch on the file, it'll update the timestamp on the file and trick CF into thinking that it changed. That's a good first step as it doesn't require altering and republishing code.
Chances are its not coldfusion that is not recompiling, it is probably the webserver itself (apache, iis, etc). If the filesize doesn't change, sometimes it wont pull the file again.
One time, I was even getting a 404 not found error on a file that I knew existed and I was beating my head on the wall trying to figure out what was happening. Turns out, the file was 404kb in size! as soon as I took out some extra whitespace and made it 403 it actually started workng again.
Sounds like trusted cache is turned on. It's very easy to flush this cache via the admin - I'd rather do that then hack around with the template itself.
You really should get to know and love your CF administrator.
@Todd - unfortunately, when on shared hosting, its just not an option.
Touching a file sounds good. I know the idea behind it, but how does one accomplish this? I develop on a Windows machine (if that helps). Does this make a difference if the "Date Modified" has already been changed? I guess the question is, what does touching it do... (Police: CFM, can you show us on the doll where he Touched you?)
I am not sure I follow this; Are you saying that IIS is caching files (forgive me, I know zero about server stuff)? I thought IIS just saw that it was a CFM request and immediately hands it off to the ColdFusion App Server?
Yeah, I have also gotten 404 errors on files that exist! So frustrating. That's a huge file you were working with. I assume it wasn't a CF file as don't they have like a 63K max size limit?
Can you give me the 2 second run down on Trusted Cache? Does that get turned on/off for a site-specific basis or for all CF apps on the server? What are the implications.
Trusted cache is simply a setting that tells the CF server that, if the template is cached, to use the cached version. Period. Otherwise, the timestamp (and maybe other properties?) are checked to determine whether to serve the cached version or recompile. It's a useful setting in production. A really annoying setting in dev environments. :-)
The setting applies to all applications running on that particular CF server.
Have you tried changing the headers? For example:
<cfset today = createDate(year(now()), month(now()), day(now()))>
<cfset yesterday = dateAdd("d", -1, today)>
<cfheader name="Expires" value="#GetHttpTimeString(yesterday)#">
@Ben - I'm pretty sure that the Trusted Cache option is server-wide. Personally, I've never enabled it... never had any sites that I didn't envision making updates to at some point.
From the CFAdministrator, under "Server Settings -->Caching"
When checked, any requested files found to currently reside in the template cache will not be inspected for potential updates. For sites where templates are not updated during the life of the server, this minimizes file system overhead. This setting does not require restarting the server."
"Save class files
When you select this option, the class files generated by ColdFusion are saved to disk for reuse after the server restarts. Macromedia recommends this for production systems. During development, Macromedia recommends that you do not select this option."
However, I've also had problems with browsers' stubborn refusal to stop caching web pages, despite me telling them not to.
@Ben: ya touch comes from the unix world, but I'm a Windows guy myself. There's a couple of ports of touch, basically alters created and last modified time stamps
I should do a blog posting for Windows CF'ers on the unix shell for Windows called Cygwin. It's like a self contained unix dos prompt that lets you plugin all the common tools from the unix world.
Handy for when you need to do stuff that can't really be done easily in Windows out of the box.
E.g, what if you wanted to find out how many lines of code your CF files have. You could do a:
find . -name "*.cf?" | xargs cat | wc -l
Thanks for the info on Trusted Cache, guys. The only concern (not really a concern) is that this is a production server - are there issues with turning this on and off in terms of drain on the server (I assume not).
But really, and this might just be me, going into the Admin, and turning off the trusted cache, seems like the same amount of work as adding lots of white space to a file. Just personal... but good to know all the options.
As far as this Touch issue... not sure if that is what it is. When things like this happen, the local date modified stamp will update. But, it seems that when the file is uploaded, this timestamp change is either not carried over to the new server or is ignored.
Just a thought - if the two servers have different clocks (times zones), do the date modified times stamps update automatically?
Trusted Cache is quite useful, and contrary to Max said enabling it doesn't mean you can't disable it later to upload a change or just clear the template cache and leave it on.
For instance it can give you a pretty substantial speed boost for complicated execution paths or framework heavy code like CS+MG+Reactor.
It's also a very important if you're pushing a large set of changes to the server. Conventionally, if you push individual files to the server it's possible that a client could get the application in an inconsistent state where one part of it was uploaded, but the other part hadn't finished yet. The more code the more likely this becomes.
One solution to this problem is to upload the files in a separate location on the server and do an atomic swap of the entire application directory. An alternative solution is to use the trusted cache and push the files to the server, then clear the template cache. This makes pushing delta changes to the server nicer too.
I can't say I've ever seen a noticeable performance hit from doing this either. I suppose the next person who requests the page is going to get a slower response, but following responses would be just as fast as before. Considering you don't have trusted cache enabled at all right now, that won't be an issue anyway.
(On a side note: Adding whitespace to a file will not work around the trusted cache. When trusted cache is enabled the server ignores the code on disk that was already loaded.)
If white space changes are ignored when Trusted Cache is turned on, then that must mean that it is not turn on on one of our production servers? Otherwise that hack, as you say, would not have any effect.
If that is the case, any ideas where the cache is coming from? Is this a date/time modified issue at that point?
The only Solution that I found was to delete the files in the Folder CFusionMX7\wwwroot\WEB-INF\cfclasses, after that everything worked perfect again
I just discovered that turning off Save class files in the ColdFusion Administrator gets rid off the Page not found errors that erroneously pop up when the page in fact does exist. I am not sure why and any assistance from someone more knowledgeable would be appreciated. =)
Another twist on this old thread.... from time to time, we *delete* a .cfm file and since the .class file still exists (we have "save class files" on), it still tries to serve it up. That is the only way I can explain it -- the file is not there, but code in it (when it was there) executes.
I cleared all the .class files (ouch... production server) and that seems to have solved the issue.
Anyone else seen that?
it is an old post but as you said unchecking Save class file helped me for the 404 error, thanks.
Adding a chunk of code really is a life saver, when trying to get rid of cached templates. Thx for the hack!