ColdFusion 8: Admin API and Trusted Cache

Along with cool tags and functions added in ColdFusion 8, there have also been some neat updates to the Admin API. If you don't know what the Admin API is - it is a set of CFCs that give you programmatic access to various administrator type functions. One example is being able to list and create datasources. Today I want to show one of the new features in the Admin API under ColdFusion 8 - being able to clear individual files from the trusted cache.

One of the caching settings you can use within ColdFusion is the trusted cache. By turning this on, ColdFusion will parse your file one time, and will never parse it again. If you change the file, even to fix something small like a typo, you have to login to the Admin and hit the "Clear Template Cache" button.

Well the Admin API lets us do this directly from code. This existed even back in ColdFusion 7. But the problem is - when you just fix one file, why do you have to clear the entire cache?

ColdFusion 8 updates the API so you can pass in a list of files. If you do, only those files are updated. This can be pretty darn useful. Here is a sample application I built that demonstrates this new API:

<cfoutput>
<form action="#cgi.script_name#" method="post">
Enter file name or directory: <input type="text" name="cachefile"> <input type="submit" value="Clear File/Folder from Template Cache">
</form>
</cfoutput>

<cfif structKeyExists(form, "cachefile") and len(trim(form.cachefile))>
   <cfset form.cachefile = trim(form.cachefile)>
   <!--- detect folder versus file --->
   <cfset filelist = "">
   <cfif directoryExists(form.cachefile)>
      <cfdirectory directory="#form.cachefile#" name="files">
      <cfloop query="files">
         <cfset filelist = listAppend(filelist, form.cachefile & "/" & name)>
      </cfloop>
   <cfelseif fileExists(form.cachefile)>
      <cfset filelist = form.cachefile>
   <cfelse>
      <cfoutput>
      <p>
      <b>You entered a file or folder (#form.cachefile#) that did not exist.</b>
      </p>
      </cfoutput>
      <cfabort>
   </cfif>
   
   <cfoutput>
   <p>
   Going to clear the following file(s) from the template cache:<br />
   <cfdump var="#listToArray(filelist)#">
   </p>
   </cfoutput>

   <cfinvoke component="cfide.adminapi.administrator" method="login" adminPassword="admin">
   <cfinvoke component="cfide.adminapi.runtime" method="clearTrustedCache" templateList="#fileList#">
</cfif>

It begins with a simple form asking you to name a file or a directory. One submitted, I check and see if you entered a folder. If you did, I do a cfdirectory to get the files in the folder. Once I have the files ready, I simply login to the Admin (note that I have hard coded the password here) and then run the clearTrustedCache function.

Pretty simple, right? There are a few updates I could do to this. I could make the cfdirectory tag recurse. I could also ask you for the ColdFusion admin password instead of hard coding it. I could even use Ben Forta's cool file browser.

If I were to package up this as something you could include in the ColdFusion admin (like SpoolMail), would folks use it?

Comments

Mmmmm, I can see this mashed up with a directory watcher gateway watching your code for changes and re-loading when a file gets tweaked. This would really be awesome for multiple CF servers sharing a codebase on a SAN or other shared source. Then you could enable trusted cache to lower bandwidth to your shared source, and any updates will auto-refresh. Now I hope the upgrade price for CF8 Enterprise doesnt break the bank, as I have to somehow sell 2 copies to a customer and 1 copy to the company I work for =)
# Posted By Justice | 6/7/07 7:06 AM
My only concern here would be the performance of having CF watch your entire box.
# Posted By Raymond Camden | 6/7/07 8:11 AM
I'll chime in-- I'd be interested in using a wrapped-up version of that API (what a smart idea on Adobe's part to allow cache-refresh for individual files). IMHO, the interface for the tool would contain both a text entry area where you could quickly paste in the path for a single file and a file dialog box for selecting multiple files.
# Posted By Tom Mollerus | 6/7/07 9:22 AM
What I'm thinking of making is maybe a tool that combines a few things. I don't see this as necessarily being an app to itself, if thats makes sense.
# Posted By Raymond Camden | 6/7/07 9:51 AM
@Ray, I agree that it wouldn't be an app to itself, but it would be nice if it could offer some sort of form interface so that you could just wrap it with an existing admin application. I'm happy to contribute code if you like.
# Posted By Tom Mollerus | 6/7/07 9:58 AM
What are you saying - my form wasn't pretty? ;) Seriously though - I'd accept any suggestions in that area.
# Posted By Raymond Camden | 6/7/07 10:05 AM
I have not had the chance to play with this, but oh so nice it looks. Curious, does cfide.adminapi.administrator.login now reuire a username? With the addition of multiple users accessing cfadmin I'd think it should.

DK
# Posted By Douglas Knudsen | 6/7/07 11:49 AM
Don't forget you can view the Admin API CFCs in your browser. If you do - you will see that username is an optional argument. The CF Admin _can_ use a username, but doesn't have to.
# Posted By Raymond Camden | 6/7/07 1:10 PM
Wonder what happens if you do this in application.cfm? If you passed a url parameter (trusted=0) could you clear the current page before the page is executed?
# Posted By Michael Long | 6/7/07 5:47 PM
You would rip a hole in the space time continuum and end the universe as we know it.
# Posted By Raymond Camden | 6/7/07 8:08 PM
COOL!!!
# Posted By Michael Long | 6/7/07 8:12 PM
... if it didn't affect the space time continuum it would (at least) cause a tremor in The Force ;-)
# Posted By David Whiterod | 6/7/07 11:08 PM
Wow, Charlie did his presentation on hidden gems for our user group and he very briefly mentioned this feature. A Google search of course returned your blog entry (which I read religiously so I'm not sure how I missed it the first time around). This is absolutely a to die for feature and I would love to see an admin add-on for it a la SpoolMail. I know you made this post quite some time ago but if you are still thinking about adding it, please do!
# Posted By Will | 12/13/07 9:04 PM
I suppose I could wrap it up sometime. I'm trying to _not_ release so many projects as it gives me too much to do. ;)
# Posted By Raymond Camden | 12/13/07 9:18 PM