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.
<!--- Kill extra output. ---> <cfsilent> <!--- Define the application. Here, I am setting a very small Application timeout (since I am on the work server) - In reality, this would either be left out (using CFAdmin default) or 2 days or something. ---> <cfapplication name="SimpleCacheDemo" applicationtimeout="#CreateTimeSpan( 0, 0, 5, 0 )#" sessionmanagement="false" setclientcookies="true" /> <!--- Set request settings. ---> <cfsetting showdebugoutput="false" /> <!--- We need to check to see if our application is initialized. For convenience, I am going to store an Applcation variable that flags for initialization (DateInitialized). If this value does NOT yet exists OR the user has flagged a reset via the URl, we are going to initialize the application. NOTE: By initializing the application using the Application.cfm (or Application.cfc) we ensure that the application get initialized no matter what page in our application gets requested. ---> <cfif ( (NOT StructKeyExists( APPLICATION, "DateInitialized" )) OR StructKeyExists( URL, "resetapp" ) )> <!--- Either a re-init of the application was requested, or the app has not yet been requested. Since we are potentially updating live, shared memory (APPLICATION), throw a Lock on it to force a single thread. ---> <cflock scope="APPLICATION" type="EXCLUSIVE" timeout="20"> <!--- Since we are in a multi-threaded environment, it is possible that why we were locking, someone else swooped in an performed the initialization before we could do it. Therefore, we need to perform a double-check lock (pattern) to see if initialization is still required. However, if we have requested a manual re-init, then we are going to proceed no matter what. ---> <cfif ( (NOT StructKeyExists( APPLICATION, "DateInitialized" )) OR StructKeyExists( URL, "resetapp" ) )> <!--- We see that, indeed, the application still is not initialized (DateInitialization flag has not been set). We can now safely proceed with application initialization. ---> <!--- Clear the existing Application to make sure we don't have any old data. ---> <cfset StructClear( APPLICATION ) /> <!--- Flag initialization. ---> <cfset APPLICATION.DateInitialized = Now() /> <!--- Now, we are going to cache some objects. For now, we are simply caching built in CF data types for demonstration. However, these could easily be ColdFusion components (or even Java objects) created using the CreateObject() function. ---> <!--- Cache this struct. By declaring the struct directly in the APPLICATION scope, we are caching in the APPLICATION scope. This struct will exist for as long as the Application does (or until the scope is cleared or the variable is deleted). ---> <cfset APPLICATION.Actress = StructNew() /> <!--- Set values in the struct. ---> <cfset APPLICATION.Actress[ "Christina" ] = "Cox" /> <cfset APPLICATION.Actress[ "Lori" ] = "Petty" /> <cfset APPLICATION.Actress[ "Linda" ] = "Hamilton" /> <cfset APPLICATION.Actress[ "Angella" ] = "Bassette" /> <!--- You could perform as much cacheing as you want at this point. I am only caching one object for demonstration purposes. ---> </cfif> </cflock> </cfif> </cfsilent>
<html> <head> <title>Simple ColdFusion Cache Demo</title> </head> <body> <h1> Here Are Your Cached Actresses (Lucky You!) </h1> <cfoutput> <!--- Loop over the actresses in the struct. We can reference the cached struct directly in the APPLICATION. Technically we are referencing shared memory, and there are those that would argue this requires a CFLock to be used... however, I am of the mind set that even the "worst case" scenario in this case will not be bad - therefore, no lock. ---> <cfloop item="FirstName" collection="#APPLICATION.Actress#"> <p> #FirstName# #APPLICATION.Actress[ FirstName ]# </p> </cfloop> </cfoutput> </body> </html>
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.
Want to use code from this post? Check out the license.