Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Disk I/O is one of most expensive operations you can perform on a server. The process of moving a hard drive platter to an empty spot, dumping data to it, and then reading that data back involves a lot of physical labor on the part of the server. Unfortunately, sometimes the only way you can accomplish something is write data to disk, such as uploading a file through the browser. But your server's RAM is fast. It doesn't have any moving parts, so why can't we use that to store some of our data? ColdFusion 9 offers a solution to some of your disk I/O issues by providing access to a virtual hard drive on the servers RAM, called a virtual file system.
In this video, I am going to show you how to use the VFS to perform file operations without writing any data to a physical disk. So the first thing you need to know before you actually start using the virtual file system is how to turn it on and how to set a size limit. And that's all done through the ColdFusion Administrator. So in the ColdFusion Administrator, if I click on Settings, and scroll down a little bit here you'll see these two options right here, Enable In-Memory File System and Memory Limit for the In-Memory File System.
So this box has to be checked before you can use it and this determines how much RAM you want to be able to use for that RAM disk. Disk memory comes out of your JVM memory, so if you're running tight on your JVM memory to begin with, you might disable this entirely. If you have got plenty of extra RAM or you are running on a 64-bit system, you can size that up as appropriate depending on how much you want to store in there. So now that we have our RAM disk ready to go, we can start putting some data in there. So I am going to go back to CF Builder and we'll open vfs.cfm.
Now, all we are going to do at this point is write a file to the RAM disk. Now, if you ever written a file to disk before, you usually just do it like so, cffile action="write". You give it a filename, which would normally be c: something or other. In this case, we're going to do ram:// and we are just going to write a test text file. And then for the Output, I am going to say 'This is a test text file' and that's all there is to it.
If I preview this in the browser, I don't get any errors, but I don't get any output either. So how do we know what's in our RAM disk? Well, the thing to note is that the RAM disk is just like a physical hard drive as far as ColdFusion is concerned. That means that I can perform all the same file operations that I can on my regular hard drive. That means that I can do a cfdirectory action = "list" directory ="ram://", and name, we're going to dump this to our RamDisk variable.
And if I dump RamDisk, preview in browser, we can see it looks just like if we were dumping the contents of our regular server hard drive. I can see that it's in the ram directory and my filename is test.txt. So how do I get data out of the RAM disk? Well, the first thing you would think you could do is cfinclude template="ram://test.txt". Unfortunately, this is going to cause a problem. If I preview this in browser, it's going to tell me that it couldn't find the template ram://test.txt.
What we need to do is set up a mapping so that ColdFusion knows where to find our RAM disk files. So if we go back to the ColdFusion Administrator, and click on Mappings, we are going to add a new mapping for our RAM disk. And let's say that the Logical Path is inmemory and the Directory Path is ram://. We'll click Add Mapping. Now I can treat the inmemory folder in my paths as the RAM disk. We'll go back to CF Builder, go back to the source code, and instead of ram here, I am going to put inmemory/test.txt.
We'll save that file, run that and preview, and there we go. It has cfincluded that test.txt and this file doesn't exist anywhere on my hard drive. Now doing this in a simple example may seem rather mundane but I can use this for anything. I could do server generated components as CFC files that get written to the RAM disk and then called using the New keyword. For example, let's assume that this was a cfc instead. I could do this, cfcset MyCFC = New inmemory.test().
And that would create a new instance of the test CFC in my RAM disk. So this is one more way that you can better manage the resources on your server. By being able to write files directly to RAM and access them as if they were any other file on disk is a huge advancement. The only thing you need to be sure you can do is limit how much you put in here, don't use it for big huge files, keep the file size as small as you possibly can. So the last thing we'll do is use one more function so that you can see exactly what's in your RAM disk in order to manage it properly.
Let's put our cfc back to txt and get rid of our cfc code here and we are going to dump out the getVFSMetadata function and tell it we want to dump metadata about our RAM disk. Right now, this is the only supported VFS available, so you have to provide RAM as the argument value. So, we'll save that file and preview it. And here we can see that it is indeed enabled, how much free space is available, what our limit is, and you notice it's not exactly 100 megabytes.
It uses the 1024 rendering of megabytes. And we can see how much is used. We just have 25 bytes used in our RAM disk. Now, there are a few limitations with what you can do with the RAM disk. For example, inmemory files have to be accessed via either a mapping or an absolute path. You can't access these via http protocols and there are some limitations around what you can do with it with cf file and the file set access mode and set attributes. For example, you can't rename a file from a RAM disk to the hard drive.
You will need to either copy it or move it. With ColdFusion 9's new virtual file system, you now have one more way to better manage your server resources. Being able to push temporary file uploads or even one-off components into the RAM disk is just as easy as using the physical hard drive. Just remember to manage your RAM usage as well as you manage your hard disk usage.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99349 Viewers
56 Video lessons · 112609 Viewers
71 Video lessons · 81429 Viewers
131 Video lessons · 39091 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.