Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best create and store APPLICATION variables?

I am using ColdFusion 9.0.1

I am taking over a site and the guy before me created about 100 variables and put them into the APPLICATION scope. I believe that his 100 variables were continuously being overwritten with each page load.

Basically, he had this in Application.cfc:

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

My plan is to keep it simple and yet very readable is to test for a specific structure in the application scope. If it's not there, 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 are done creating all of the application variables, I'd move this into the into the onApplicationStart() method.

The solution that I want has to be more about "readability" and less about "efficiency". Several non-CFers, but very experience coders will be using this and will need to "get it" quickly.

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

Is there a more readable way of creating and managing application variables?

like image 798
Evik James Avatar asked Nov 28 '11 20:11

Evik James


People also ask

What are application variables?

Application variables are values that are available to any user or page within an application. For the first time, they can be any value you wish to store.

Why and which session variable is more suitable for Web application?

Session variables are useful for storing information, such as login names or other user data, but using a Session variable costs memory overhead. Session variables make it easy for you to store and manipulate data.

What variable can you use to share info across the whole application for one user?

Session variables are variables which remain common for the whole application but for one particular user. They also can be used across the whole application but each user will have a copy… But they die when a particular user session ends or probably when they are killed forcibly…


2 Answers

Why not move the definition into onApplicationStart() right now? If you need to reset them during development, you could always pass in a URL variable to flag it for reset, like so:

<!--- in Application.cfc --->

<cffunction name="onRequestStart">
<cfif IsDefined("url.resetApp")>
  <cfset ApplicationStop()>
  <cfabort><!--- or, if you like, <cflocation url="index.cfm"> --->
</cfif>
</cffunction>
like image 70
Jake Feasel Avatar answered Sep 29 '22 05:09

Jake Feasel


Actually, after re-reading the OP, and reading the suggested solutions, I'm going to have to agree with the OP on his setup, for this very important reason:

This, in onApplicationStart()

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

Can then later be turned into this, within onRequestStart()

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

Your app can then go on to access the REQUEST vars conveniently, esp, if you decide you want to cache CFCs in the same scope--they would simply go into a separate key:

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

Which, of course, also gets poured into REQUEST (if you choose)

Want to go Jake Feasel's route above? No problem:

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

Now you're able to flexibly kill your object cache but maintain your vars (or vice versa as you choose).

This is a great setup for another reason: If you want to build in your own Development/Production "mode", in which the development mode always recompiles the CFCs, but the production mode keeps them cached. The only change you have to make on top of this, is the REQUEST set 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 make the setting of vars and the creation of objects into a private method within Application.cfc, for even further convenience.

like image 31
Shawn Holmes Avatar answered Sep 29 '22 06:09

Shawn Holmes