There is no particular question here. I have been asked about variable caching in ColdFusion and I felt a demo would explain what a back-and-forth email conversation could not. This is a demonstration of very simple variable caching in a small ColdFusion application. Here, we are caching a value (a Struct in this case) in the APPLICATION scope. We are then referencing it on the index.cfm page.
The demo has two pages. The first is our Application.cfm page. I used the Application.cfm rather than the Application.cfc as I feel for demonstration purposes, there is less "noise" in the Application.cfm page. Then, we have our index.cfm page. The index page merely references the cached variable and displays the data.
Launch code in new window » Download code as text file »
Launch code in new window » Download code as text file »
I am sure this is pure review for many of you, but it may be new to even more of you. Please let me know if any further explanation is needed.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Nicely done, Ben. One clarification: the cflock and double-check for if NOT StructKeyExists( APPLICATION, "DateInitialized" ) is *only* necessary because the StructKeyExists( URL, "resetapp" ) is included, correct? If we didn't want to allow re-initialization of the Application-scoped variables via the URL (for security reasons), then the cflock and double-check are unnecessary in CF 6.1+.
Posted by Aaron Longnion on Mar 28, 2007 at 10:00 PM
@Aaron,
I actually made one small mistake. In the inner CFIF statement, I forgot to check for the manual reset. I have updated the code (see the CFIF inside the CFLock). If someone requests a manual reset, the DateInitialized IS going to be present which is why the original code wouldn't work.
And to answer your question, Yes, the double-check locking is required ONLY because of the manual reset. If this code was fired because the application was just starting up, theoretically, this would be a single-threaded environment. But actually, as I am even typing this, I am not so sure. Because these are based on page requests (the Application.cfm does not get processed until a page is requested) then I actually don't see why two simultaneous requests couldn't cause conflicts - see, checking for DateInitialized existence and then setting the value has some time between it (nanoseconds??). Is it possible for two threads to overlap in this action???? I was have to say Yes to be cautious.
So, long story short, when using Application.cfm, I would recommend always doing the double-check locking. But, if you are using the Application.CFC with OnApplicationStart(), ColdFusion takes care of single-threading that on application start. Of course, if you put a manual reset in that, I would again, perform a double-check lock.
Posted by Ben Nadel on Mar 29, 2007 at 7:20 AM
Ben,
Why not perform the CFLOCK first and then do the check to make sure the app is not already initialized. If it is then bail on the lock otherwise complete the initialization action?
Ron
Posted by Ron Alexander on Mar 29, 2007 at 12:06 PM
@Ron,
If you do the CFLock out side of the CFIF statements that you force a portion of the Application.cfm to become single-threaded regardless of whether or not any app-initialization is going to take place. CFLock comes with slight overhead; for small sites, this might not matter, however, for a heavily trafficked site, this slight overhead on every single page request might start to slow things down.
Would people notice the slow down? Not sure, can't say. But, as a generally good practice, I would say you want to avoid locking whenever possible.
Posted by Ben Nadel on Mar 29, 2007 at 12:23 PM