What is the best way to create and store APPLICATION variables?

I am using ColdFusion 9.0.1

I take over the site, and the guy in front of me created about 100 variables and put them in the REQUEST area. I believe that its 100 variables are constantly rewritten each time the page loads.

Basically, he had it in Application.cfc:

APPLICTION.VariableOne = "SomeStringOne"; APPLICTION.VariableTwo = "SomeStringTwo"; APPLICTION.VariableThree = "SomeStringThree"; 

My plan is to keep it simple and yet very readable in order to check the specific structure in the application area. If not, create the structure and variables:

 if (not isDefined("APPLICTION.AppInfo") or not isStruct(APPLICTION.AppInfo)) { APPLICTION.AppInfo = structNew(); APPLICTION.AppInfo.VariableOne = "SomeStringOne"; APPLICTION.AppInfo.VariableTwo = "SomeStringTwo"; APPLICTION.AppInfo.VariableThree = "SomeStringThree"; } 

Of course, once the site is live, and we have finished creating all the application variables, I would move it to the onApplicationStart () method.

The solution I want should be more about readability and less about efficiency. A few non-CFers, but very experienced coders will use this and will need to "get it fast."

Does my plan have any gaping holes or is it too inefficient?

Is there a more readable way to create and manage application variables?

+4
source share
3 answers

Actually, after reading the OP again and reading the proposed solutions, I will have to agree with the OP to configure it for this very important reason:

This is in onApplicationStart ()

 APPLICTION.AppInfo = structNew(); APPLICTION.AppInfo.VariableOne = "SomeStringOne"; APPLICTION.AppInfo.VariableTwo = "SomeStringTwo"; 

It can then be drawn to this, inside onRequestStart ()

 <cflock name="tmp" type="readonly" timeout="15"> <cfset REQUEST.AppInfo = APPLICATION.AppInfo /> </cflock> 

Then your application can quickly go to the REQUEST windows, for example, if you decide that you want to cache CFC in the same area, they simply go to a separate key:

  APPLICATION.Model.MyObject = CreateObject('component','myobject'); 

Which, of course, also translates into a REQUEST (if you choose)

Want to go the Jake Feisel route higher? No problems:

  <cfif isDefined('URL.reload')> <cfset APPLICATION.Model = StructNew() /> </cfif> 

Now you can flexibly kill the cache of objects, but keep your vars (or vice versa, as you choose).

This is a great setting for another reason: if you want to create your own development / production mode in which the development mode always recompiles CFC, but the production mode saves them in the cache. The only change you should make on top of this is the REQUEST noted above:

 <cfif (isProduction)> <cflock name="tmp" type="readonly" timeout="15"> <cfset REQUEST.AppInfo = APPLICATION.AppInfo /> </cflock> <cfelse> <cfset REQUEST.AppInfo = StructNew() /> <cfset REQUEST.AppInfo.VariableOne = "SomeStringOne" /> ...etc... </cfif> 

You can also configure vars and create objects as a private method in Application.cfc for even more convenience.

+1
source

Why not move the definition to onApplicationStart () right now? If you need to reset them during development, you can always pass a URL variable to mark it for reset, for example:

 <!--- in Application.cfc ---> <cffunction name="onRequestStart"> <cfif IsDefined("url.resetApp")> <cfset ApplicationStop()> <cfabort><!--- or, if you like, <cflocation url="index.cfm"> ---> </cfif> </cffunction> 
+4
source

I would go ahead and just use OnApplicationStart, but in the previous days of Application.cfc we used something like Application.Build, and if the Build value was different, we executed all of our Application variable sets. So fast and dirty would be something like:

 <cfparam name="Application.Build" default="" /> <cfset Build = "28-Nov-2011" /> <cfif Application.Build IS NOT Variables.Build OR StructKeyExists(URL, "Rebuild")> <cfset Application.Build = Variables.Build /> <!--- A bunch of other CFSETs ---> </cfif> 

This method was what we used when all we had was Application.cfm

+1
source

Source: https://habr.com/ru/post/1383513/


All Articles