Posted in ColdFusion | Posted on 10-14-2010 | 2,669 views
So a few days ago I wrote a quick blog entry asking why more people weren't making use of Trusted Cache. While not a silver bullet, it can sometimes be a quick way to improve your site performance. A reader, johans, commented asking what the impact would be on files stored in the virtual file system (VFS). I assumed it would act the same - ie, any changes would not be picked up. Today I wrote a quick template to see if I was right:
2<cfset content = "<cfoutput>Your num is #num#</cfoutput>">
3<cfset fileName = "ram://foo.cfm">
4<cffile action="write" file="#fileName#" output="#content#">
5
6<cfoutput>My number is #num#<p/></cfoutput>
7
8<!--- note, RAM mapping was made in cf admin --->
9<cfinclude template="/RAM/foo.cfm">
When run, this code creates a random number and writes the CFML to display it to a file in the vfs. Yes, the code is dumb. There is no need to use cfoutput in this case. But I wanted to ensure I had some dynamic code in the VFS. After writing it, we output the number and include the template. Don't forget you can't cfinclude a file via a full physical path. I made a mapping in the CF Admin for my VFS called RAM and included it like that.
Before turning on trusted cache, I ran this template a few times and saw what I expected - the same number in both outputs. After turning on the trusted cache, the number in the VFS "froze" since ColdFusion wasn't picking up on the changed CFM. (Which is expected - this isn't a bug.) To be clear, ColdFusion is still writing the file out and changing it. That part works fine. It's just the compiled version of the CFM that is being cached and reused. Clearing the trusted cache immediately brought out the new number.
So what if you want to make use of the VFS like this and make use of the trusted cache? Well if we assume you aren't writing to a static file on every request, you can simply use the Admin API to clear it. If you do need something for every request you could consider unique file names. Just be sure to "clean up" or your VFS will fill up eventually.


Don't forget though that there are ways around it.
Two cases I see are:
1. On the server I have a file or files that I create to run a quick script against the database or other code, and usually I write it and test as I go. It's a pain to clear that file each time I fiddle with it. (In this case I guess I could write a custom tag to include to try to clear the calling file from the cache.. or something.)
2. Most of the code doesn't change, but there may be a site in progress, any way at all to say cache all but this folder?
Thank you for your awesome tips.
I think Railo has a function to run CFML without having to resort to cfinclude.
<cfset createObject("component","cfide.adminapi.administrator").login(":-p")>
<cfinvoke component="cfide.adminapi.runtime" method="clearTrustedCache" templateList="#GetBaseTemplatePath()#">
I think it should work, cause CF would cache the code.. and then run it, and the running would uncache it..
The error occurred in runtime.cfc: line 661
Called from E:\wwwroot\tmp\dontCache.cfm: line 3
Called from E:\wwwroot\tmp\___notes___.cfm: line 4
-1 : Unable to display error's location in a CFML template.
I feel like I'd need to do some kinda of after the fact delayed execution (onRequestEnd?), but maybe it's not quite worth it.
<cfset ob = createOb....
Then in line to change component="..." to component="#ob#". Not sure that will work - but it may.
One CMS that I know of saves bits and pieces of pages out as CFM files and includes them as needed.
Which works fine until a user posts an update and the CMS writes out a new file... which is then ignored by a system with Trusted Cache enabled.
[Add Comment] [Subscribe to Comments]