Template Caching Revisited

2007 December 02
tags: ColdFusion · Code
by Nick

One of the first snafu's we encountered upon launching my latest project was that the template caching approach didn't hold up under load in production. The primary reason for this was the use of the cfdirectory tag to check for file existence and date modified. This was a performance bottleneck largely caused by unnecessary overhead. Lesson Learned: this is one tag not ideal for use under heavy-load.

Thanks to this post, I was able to re-write the file checking in Java and improve performance significantly.
I also replaced cffile with java which improved things even more. Here is the code:

In place of cfdirectory: <!--- get date modified using java for compare --->
<cfset fileObj = createObject("java","java.io.File").init(variables.myFile)>
<cfset fileDate = createObject("java","java.util.Date").init(fileObj.lastModified())>
In place of cffile action="write": <!--- write to file, use java for performance gains --->
<cfscript>
cacheFile = createobject("java", "java.io.File").init(myFile);
cacheStream = createobject("java", "java.io.FileWriter").init(cacheFile);
cacheStream.write(trim(cacheContent));
cacheStream.flush();
cacheStream.close();
</cfscript>

Something else that came out of this... the execution times for these tags vary greatly based on the server platform. While my load tests proved that this caching approach performed well under load in our development environment (Windows/IIS), it ran many many times slower in our production environment (Solaris/Sun One).
I haven't investigated the cause of this, but it is yet another reason that we need to replicate our dev environment and be sure we are developing/testing code on the same platform running in production. (I know many will be appalled by this admission, but using a different platform for testing and development was just a temporary solution which turned into longer-term due to lack of resources)

No Responses leave one →

Leave a Reply