Is the application.cfc static in memory?
-
21-07-2021 - |
Question
I'm experimenting with the Application.cfc within coldfusion. I'd like to know if the application.cfc is static in memory, i.e it's created once for the first user and there after every user that accesses it accesses the same application.cfc (within memory).
Example. UserOne accesses webpage > Application.cfc is created at memory block 1. UserTwo accesses webpage > Application.cfc at memory block 1 is called again but function onRequestStart is called.
Am I correct in saying that the application.cfc is static within the memory (or until it expires) or is it recreated for every single user? Would this be a huge memory issue if it was?
Can someone explain thanks.
Solution
Application.cfc is executed for every request but only parts of it are run depending on the situation. The pseudo-constructor (where you set this.name type settings) is executed each time and cannot be changed problematically. onApplicationStart() only runs if the application doesn't exist. The application scoped variables are accessible to every session and only exist once per application instance (not session instance). onSessionStart() only runs the first time a new visitor hits the site. There are other event specific functions
Here is another thread that may may help with your question.
As well as adobe docs:
http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-74fa.html
An active user counter is as easy as something like pseudo code:
onApplicationStart {application.activeUsers = 0}
onSessionStart {application.activeUsers++}
onSessionEnd {application.activeUsers--}
Clearing up some confusion
The 'this' scope is used to set application settings such as the name, sessionTimeOut, or customTagPaths. These settings are built into ColdFusion.
<cfscript>
this.customtagpaths = expandPath('./customtags');
this.name = "myCoolWebsite";
this.sessionmanagement = "Yes" ;
this.sessionTimeOut = CreateTimeSpan(0,0,20,0);
</cfscript>
Although the 'this' scope is related to the application, you cannot use it to set persistent application scope variables. Application variables are set by using the 'application.' syntax and is usually initially set in the onApplicationStart() function.
<cfset application.myVariable = "I am the same value for every user">
OTHER TIPS
Your assumptions are correct
Application events are specific occurrences during the life cycle of an application. Each time one of these events occurs, ColdFusion runs the corresponding method in your Application.cfc file (also referred to as the application CFC). The Application.cfc file defines application settings and implements methods to handle the application events.
Application variables are available to all pages within an application, that is, pages that have the same application name. Because application variables are persistent, you easily can pass values between pages.
Also worth noting
The cflock tag controls simultaneous access to ColdFusion code. The cflock tag lets you do the following:
- Protect sections of code that access and manipulate shared data in the Session, Application, and Server scopes, and in the Request and Variables scopes for applications that use ColdFusion threads.
...