Should static files be saved as TXT or CFM or HTML?
We have a process that creates tens of thousands of static files. Each file is generated from a request that takes a few seconds. The data in each file is updated weekly. These files are served about 500,000 times a day. As such, it seems like it would be very helpful to name and save and retrieve these files as efficiently as possible.
Files are currently named like this:
123-111-1.htm
123-112-1.htm
123-113-1.htm
456-111-2.htm
456-112-2.htm
456-133-2.htm
The files contain plain old HTML menus. Nothing is processed on this page. There might actually be fifty links.
<h1>Company Name</h1>
<ul>
<li><a href="index.cfm">some link</a></li>
<li><a href="index.cfm">some link</a></li>
<li><a href="index.cfm">some link</a></li>
<li><a href="index.cfm">some link</a></li>
</ul>
We include the following menu:
<cfset FileToInclude = "#var1#-#var2#-#var3.htm">
<cfinclude template="#FileToInclude#">
My real question is, would there be a difference between saving the file as txt, or html, or cfm? With a different file extension, will it be handled differently by the server? If it has a txt extension, does ColdFusion completely ignore it and not cache it? If it has an html extension, won't it get parsed but cached?
source to share
Let's see what each of the options entails
TXT
<cfinclude template="#FileToInclude#.txt">
We know that the content is not plain text, so the extension of this file will not describe its content. You can configure the web server to not display files .txt
. Internally, ColdFusion will treat it as if it had tags, which may not be what you expect.
Html
<cfinclude template="#FileToInclude#.html">
This is a more detailed description of the content. The web server will most likely try to display it. Will be treated in ColdFusion as if it had tags that might not be what you expect
CFM
<cfinclude template="#FileToInclude#.cfm">
This is a description of what really works. You can make sure it never runs as a separate file. Since it has an extension .cfm
, you would expect it to be processed by ColdFusion.
As far as caching goes, I think ColdFusion caches them anyway.
db
You should also consider storing data in db
<cfquery name="qryMenu" cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#">
SELECT menu
FROM dbo.menu
WHERE section1 = <cfqueryparam value = "#section1#" cfsqltype = "cf_sql_integer">
AND section2 = <cfqueryparam value = "#section2#" cfsqltype = "cf_sql_integer">
AND section3 = <cfqueryparam value = "#section3#" cfsqltype = "cf_sql_integer">
</cfquery>
<cfoutput query="qryMenu">#menu#</cfoutput>
This way you don't have to deal with hundreds of menu files. You can control the caching exactly the way you want. And that will probably put less load on your server.
source to share
If you include the file, CF will parse, compile, and process it. If there is no CFML in the file, it is a waste of time. It will also place the resulting compiled class in server memory, which is another minor consideration but will be considered nonetheless.
You must specify your file for the file content. These files have HTML code in them, so they must have a .html extension.
If you just want to get the contents of a file on screen, just read the file and output it:
<cfoutput>#fileRead("/path/to/file")#</cfoutput>
As of ColdFusion 11, there is some tweak where files that are not associated with the .cfm extension will not compile when enabled, but they are compiled with CF10.
source to share