ColdFusion Application.cfc - execution order

I need a reality check - and, hopefully, an explanation (if my reality is wrong).

The way CF application framework evaluates things is (my understanding) - the request is passed to cfserver

  • cf finds application.cfm or cfc (based on move rules)

  • application.cfc is executed (if found)

  • the IT area is set (a series of specific applications can be set here, but

some are required - for example, applicationTimeout - then a series of events occurs - and if necessary, methods are launched.

- onApplicationStart ()

---- onSessionStart ()

------ onRequestStart ()

and etc.

so my questions

1) DATA settings are performed at each page request - before anything else?

2) If I set the application variable in onApplicationStart () - it is available in any process that occurs after that - And it must be stored in memory for the length of applicationTimeout () - right?

3), so if I do something like this ...

if (isdefined ("application.myvar") {this.something = application.myvar;}

SHOULD work on any page request after the initial request that launched the scope.

however, this does not seem to be the case.

My reason for the request is that there are some interesting application leverage settings that need to be set in the IT area ... some of them can be "intensive" (at least from the point of view of execution on each request - so I want to do them only ONCE, set the structure to permanent mem, and then use those that are available as IT.

Am I making the wrong assumptions?

THX

+8
coldfusion
source share
5 answers

The ColdFusion Application.cfc documentation contains this information:

When the request is executed, ColdFusion runs the CFC methods in the following order:

  • onApplicationStart (if not previously run for this application)
  • onSessionStart (if not previously performed for this session)
  • onRequestStart
  • onRequest / onCFCRequest
  • onRequestEnd

OnApplicationEnd, onSessionEnd and onError CFC are triggered by certain events.

In the general order of the request, there are (at least) two more steps.

0: execute all code in cfcomponent which is not in cffunction
0.5: execute cfapplication tag cfapplication to create application

Thus, the answers to your questions:

  • If you set these variables in step 0, then yes.
  • Correctly.
  • It depends on where you set the variable. If the values ​​you are trying to change are listed on the Application variables documentation page for Application.cfc, then they should be at step 0. Setting them elsewhere will update the this area, but will not take effect at step 0.5.
+3
source share

There are two things here: when the code is executed, and when the variable areas are available and how long they last.

  • Code with any method (ie: "pseudo-constructor") starts each request. Obviously, minimize the amount of code in this part of CFC.
  • The code in various event handlers is executed as indicated in the name of the event handler, for example: onApplicationStart () code is run only once when the application starts. Ditto onSessionStart () is run only once per new session.

Areas of use:

  • This area is available throughout CFCs. It behaves exactly like this area in any other CFC, except that the variables of this range have a special meaning (for example, this.name , this.datasource , etc.). These special value variables can be changed per session or per request in the corresponding handlers, but it seems to apply to the entire system (that is, not for a specific session or request making a change in settings). In ordinary CFCs, the this scope is used to expose public variables, but since there is no open instance of Application.cfc, it makes no sense to use this scope outside of the special settings. If you want variables to be accessible to all methods inside CFC, use the variable area as usual. Unlike some guidelines, scope variables are not the same as scope variables.
  • The request area is also available on all CFCs (pseudo-constructor and methods). They will also be available for the calling code of the templates, called later in the request, like any other variables with the scope of the request.
  • Application scope: not available in the pseudo- this.name even after setting this.name . It only becomes available in onApplicationStart () and from there from there.
    • Session scope: similarly, not available in the pseudo-constructor or onApplicationStart () l, not onSessionStart ().

I demonstrated this in a blog post (provided test code), here . it is too long to be included here, but the material above summarizes it.

+2
source share

Please review the comments as it will look below, but it is not. If you reset this area, a new value is displayed, but in fact it does not change any application settings.

You can change any application settings anywhere; however, since the pseudo-constructor starts every time a page is requested, you will need to constantly change the setting after starting the pseudo-constructor. The application area is not available in the pseudo-constructor, so you can do this in the onRequestStart or onRequest functions. I did a simple test reassigning customtagpaths to a condition in the onRequestStart function. You will notice that the first time you access the page, custom tag folders will add additional customtags indicating "someOtherCustomtagsFolder". On the side of the note, if your application settings are changed for each user, your global application settings will be flip-flopping and may cause problems with incorrect settings of other users.

 <cfcomponent> <!--- pseudo constructor ---> <cfset this.customtagpaths = expandPath('./customtags')> <!--- onRequestStart ---> <cffunction name = "onRequestStart" returnType="void"> <cfif structKeyExists(application,'testSetting')> <cfset this.customtagpaths = expandPath('./someOtherCustomtagsFolder')> </cfif> </cffunction> <!--- onRequest ---> <cffunction name = "onRequest" returntype="void"> <cfargument name="targetPage" type="String" required = "true" /> <cfdump var = "#this#" label = "this"> <cfset application.testSetting = "foo"> <cfinclude template="#Arguments.targetPage#"> </cffunction> </cfcomponent> 
+1
source share

Everything that is in this area inside the Application.cfc file becomes a varialbe application and is created only once per application life cycle. After starting the application, there is no other user for this in Application.cfc.

When the CF application is launched for the first time, the contents of onApplicationStart () starts up to onRequest / Start / End (with the exception of "new in CF10" onServerStart ()).

Any application variables set anywhere in the application exist until the application is stopped.

Your code from # 3 should be

if ( !structKeyExists( application, "myvar" ) { application.myvar = foo; }

then specify application.myvar where you need it.

Nothing from your description needs to be added to the this area, you just need to put it in the application area.

-one
source share

the scope is not available in the pseudo-constructor Application.cfc, because until this.name is set, it is impossible to associate the request with the application.

If you are concerned about the overhead of creating application mappings, one of them will be to cache them in an accessible area of ​​the server.

if (! structkeyexists (server, 'myappmappings')) {server.myappmappings = createMappings (); } this.mappings = server.myappmappings;

you can also use cachePut / cache Get to store mappings in ehcache, but I have not tried this in the pseudo constructor.

-2
source share

All Articles