1

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.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72

2 Answers2

6

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.

ColdFusion Application.cfc - order of execution

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">
Community
  • 1
  • 1
genericHCU
  • 4,394
  • 2
  • 22
  • 34
  • I tried creating an application variable that increments on each request, it gets reset every-time the page is refresh or a new user accesses the page. Surly this would mean that the application is not static? – Abakiz Myth Oct 30 '12 at 10:42
  • Make sure you enable session management. If you've done that, I'd have to see code to answer the reason why your application variable gets reset. – genericHCU Oct 30 '12 at 10:44
  • to enable session management: if you're using cfscript use this.sessionmanagement = true; If you're using tags both belong at the top of your application.cfc file outside of a function just after your tag. – genericHCU Oct 30 '12 at 10:48
  • Ahh you see I was using This.ClientCount within the application class, any idea why this would be reset? – Abakiz Myth Oct 30 '12 at 10:51
  • the 'this' scope is for setting application specific settings that coldfusion expects such as this.name, this.customTagPaths, etc. It can't be used to set application scope variables. To do that you use application.userDefinedVariable. this.userDefinedVariable is ignored (I believe). – genericHCU Oct 30 '12 at 10:56
0

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.

Source

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.

Source

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.

...

Source

John
  • 13,197
  • 7
  • 51
  • 101
  • So how would I create a variable that changes for all users within the page? Say a hit counter. – Abakiz Myth Oct 30 '12 at 10:38
  • If you don't ever want to lose the hit counter due to CF going down or the server rebooting, you would store it in the database. If you don't mind losing the hit counter from time to time, then the `application` scope would be appropriate. – Chris Peters Oct 30 '12 at 10:42