One of the great new features in ColdFusion 9 that I haven't seen much press about is it's Virtual File System. The virtual file system is essentially a RAM disk (remember back to DOS?). This allows you to do three really cool things. First. you can now write files such as images, spreadsheets, etc. to memory instead of disk before serving them back to the browser. Here's an example from the beta docs that shows this in use for writing a JPG file to memory and serving it up:

view plain print about
1<cffile action="readBinary" variable="myImage" file="#ExpandPath('./')#/blue.jpg">
2<cffile action="write" output="#myImage#" file="ram://a.jpg">
3<cfif FileExists("ram://a.jpg")>
4 <cfoutput>a.jpg exists</cfoutput>
5<cfelse>
6 <cfoutput>a.jpg Doesn't exists</cfoutput>
7</cfif>

The second thing this lets you do is write dynamic .cfm files to memory and execute them. Again from the beta docs, to write a file you would do something like this:

view plain print about
1<cffile action="write" output="#cfml#" file="ram://filename.cfm"/>

How you use/execute an in-memory file depends on whether the tag/function you are using requires a relative or absolute path. For tags/functions that require a relative path, you need to first create a mapping for ram:// in the ColdFusion Administrator. Once you've done that, you simple use the mapping in the relative URL. For example if you create a mapping called /inmemory, you would use it within cfinclude like this:

view plain print about
1<cfinclude template="/inmemory/filename.cfm">

For tags/functions that take an absolute path, the syntax is straightforward. From the beta docs:

view plain print about
1<cffile action="append" file="ram://a/b/dynamic.cfm" output="I'm appending">

The third thing you can do with the virtual file system is write and execute CFCs in memory. To write a CFC to the virtual file system you do the following, from the beta docs:

view plain print about
1<cffile action="write" output="#cfcData#" file="ram://filename.cfc"/>

You execute the CFC like so:

view plain print about
1<cfset cfc=CreateObject("component","inmemory.filename")/>

There are some limitations to the ram based file system. First and foremost, you can't write Application.cfm or Application.cfc to memory. Additionally, paths are case-sensitive.

The full list of tags that support the virtual file system are as follows:

  • cfcontent
  • cfdocument
  • cfdump
  • cfexchange
  • cfexecute
  • cffeed
  • cfhttp
  • cfftp
  • cfimage
  • cfloop
  • cfpresentation
  • cfprint
  • cfreport
  • cfzip

Supported file functions:

  • FileIsEOF
  • FileReadBinary
  • Filemove
  • Filecopy
  • FileReadLine
  • FileExists
  • FileOpen
  • FileWriteln
  • FileClose
  • FileRead
  • FileDelete
  • DirectoryExists
  • FileSetLastModified

So, what do you all think? I think this opens up a lot of interesting possibilities, especially in terms of performance improvement.