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?