ColdFusion 9.0 Resources |
Working with in-memory filesContents [Hide]Memory-based virtual file system speeds up the processing of transient data. In-memory files are not written to disk and are saved on RAM. They function similar to disk files but perform faster. In ColdFusion, in-memory files help you to simplify the execution of dynamic code. In-memory files are supported across almost all tags and functions that take file or directory as input or output. You use in-memory files in the same manner as files on disk, but with a prefix ram:/// to indicate that they reside on RAM. For example, ram:///a/b/dynamic.cfm. Writing and executing dynamic CFM filesThe following syntax explains how to write CFM data in
to an in-memory file:
<cffile action="write" output="#cfml#" file="ram:///filename.cfm"/> The following sample syntax explains how to use the in-memory CFM file:
Note: You cannot have Application.cfm as an in-memory file. If
you have one, it is ignored.
ExampleThe following code describes how to write an image as an in-memory file: <cffile action="readBinary" variable="myImage" file="#ExpandPath('./')#/blue.jpg"> <cffile action="write" output="#myImage#" file="ram:///a.jpg"> <cfif FileExists("ram:///a.jpg")> <cfoutput>a.jpg exists</cfoutput> <cfelse> <cfoutput>a.jpg Doesn't exists</cfoutput> </cfif> Writing and instantiating dynamic CFC filesThe following syntax explains how you can write CFC code in to an in-memory file: <cffile action="write" output="#cfcData#" file="ram:///filename.cfc"/> The following sample syntax explains how you can instantiate the in-memory CFC file: <cfset cfc=CreateObject("component","inmemory.filename")/> Here, inmemory is the ColdFusion mapping that points to ram:///. Note: You cannot have Application.cfc as an in-memory file. If
you have one, it is ignored.
ExampleThe following code writes a CFC as in-memory file: <cffile action="read" file="#ExpandPath('./')#/dynamic.cfc" variable="Message"> <cffile action="write" file="ram:///cfc/dynamic.cfc" output="#Message#"> To invoke a component method: <cfinvoke component="inmemory.cfc.dynamic" method ="method1" returnVariable="returnVariable"> <cfinvokeargument name="paramOne" value="hello"> </cfinvoke> <cfoutput>#returnVariable#</cfoutput> Working with in-memory file systemThe following sections provide information that can help you to access and use in-memory files. Using in-memory files
Supported functionsThe following file functions are supported for in-memory files:
ExampleThe following syntax explains the function FileSetLastModified() <cftry> <cffile action="write" file="ram:///a.txt" output="Testing the function FileSetLastModified"> <cfset date="12/12/2007"> <cfscript> FileSetLastModified("ram:///a.txt", "#date#"); sleep(1000); WriteOutput(#GetFileInfo("ram:///a.txt").lastmodified#); </cfscript> <cfcatch> <cfset PrintException(cfcatch)> </cfcatch> </cftry> <cf_expectedresults>{ts '2007-12-12 00:00:00'} </cf_expectedresults> File operationsThe following file operations are supported for in-memory files:
ExampleThe following code illustrates the file and directory operations: <cfdirectory action = "create" directory = "ram://src" > <cfdirectory action = "create" directory = "ram://des" > <cfdirectory action = "rename" directory = "ram:///CurrentDir" newDirectory = "NewDir"> <cfdirectory action="list" directory="ram://" name="listDir" recurse="yes" > <cfdump var="#listDir#"> <cffile action="write" file = "ram://src/test.txt" output = "Release Description"> <cffile action="copy" source="ram://src/test.txt" destination="ram://des/final.txt" > <cffile action="rename" source = "ram:///src/message.txt" destination = "ram:///des/test.txt"> <cffile action ="move" source = "ram:///des/test.txt" destination = "c:\des\move.txt"> Document and image actionsAll image and document actions can use in-memory image
files as shown in the following examples:
<cfimage action="captcha" fontSize="15" width="180" height="50" text="readMe" destination="ram:///readMe.jpg" difficulty="medium"> <cfimage source="ram://aiden02.png" action="convert" destination="#ExpandPath("./")#/blue1.JPG" overwrite="yes"> <cfdocument format="pdf" filename="ram://Sample.pdf" overwrite="yes">Sample Text</cfdocument> Custom tagsIn-memory CFM pages and CFCs can call custom tags but the custom tags must be present in disk. In-memory custom tags are not supported. Using in-memory files in tagsThe following tags are supported for in-memory files:
Example using the tag cfcontent<cfcontent file="ram:///a.jpg" type="image/jpeg" deletefile="yes"> Adding permissionsColdFusion lets you add permissions for directories/files on RAM using an existing sandbox security setup. You can only set up sandbox security for disk directories and not for RAM directories. Access to an in-memory directory/file can be restricted only through an existing local file system sandbox. Therefore, to set up sandbox security for in-memory files, select a sandbox that you have already configured for a disk directory. By default the ram:/// directories are included in the list of secured folders and have read, write, execute, and delete permissions set. Therefore, all RAM files have permissions by default in a sandbox. All the security restrictions that apply to disk files apply to in-memory files. To set up Sandbox security for in-memory files,
For further details on sandbox security, refer to the ColdFusion Administration Guide. Accessing VFS informationThe GetVFSMetaData function lets you access VFS information. This function returns a structure that contains the following information:
Deleteing in-memory filesThe in-memory files remain in RAM as long as the server is up. When required, clean up the files using cffile/cfdirectory with the action=delete. For example, delete all the contents in RAM directory "ram://a/b" using the following code: <cfdirectory action="delete" directory="ram:///a/b" recurse="yes"> Limitations
|