I was looking for a good way to do this and came across Brendan O'Hara's article on the Singleton pattern. <http://www.sys-con.com/coldfusion/article.cfm?id=624>. This is essentially what I am looking for, but I do not see how this strategy will increase operational efficiency for my function library.
The article basically says that you run a line of code such as
<cfset x=createObject("component","components.maintenance").init()>.
Inside the init() function, you have all the locking and existence checking. If the server CFC does not exist, you create it. The init() function returns the server CFC.
My understanding of what happens is that a local copy of the maintenance CFC is instantiated, then immediately replaced by the CFC that is already in the server scope. However, this seems to defeat the purpose of caching the CFC in the server scope, which is to prevent me from having to instantiate the object. In fact, since the CFC only consists of static methods, the local CFC that has a brief life would be exactly the same as the CFC in the server scope.
To create a singleton object without any wasted instantiations, I was thinking that the better strategy would be to cfinclude a file which contains the following code:
<cfif NOT structKeyExists(server,'maintFunctions')>
<cflock timeout="10" throwontimeout="No" name="maintFunctions" type="EXCLUSIVE">
<cfif NOT structKeyExists(server,'maintFunctions')>
<cfif NOT structKeyExists(server,'maintFunctions')>
<cfset server.maintFunctions=createObject("component","components.maintenance").init()>
</cfif>
</cfif>
</cflock>
</cfif>
I timed both methods by running them through a loop 1000 times. The first "createObject" version takes 16 seconds, while the second "cfinclude" version takes 2 seconds. Clearly the method where the server instance is returned by the init() function is very wasteful, and if I were to implement a cached CFC, I would cfinclude the code to handle the existence checking.
So my question is, what is the best way to have a library of functions in CFMX 6.1? Would my CFC stored in the server scope be more efficient than custom tags or cfincluding a UDF file? As far as manageability, I think all three methods are roughly equal, although with a cached CFC I will have to delete the server variable whenever I make a change to the file.
Thank you, Mike Chabot
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]
