Reputation: 73
I've been noticing on my CF server that my apps are starting to get quite slow. I'm attributing much of this to not caching some things, when indeed they should be cached.
I would like to verify that this object is in the application scope and not create it every time the page loads.
<cfset application.components.blog = createObject('component','sharedcomponents.cfc.blog').init()>
What would be the best way to verify if the object exists within the Application scope and if it is let it go. Also if the files have been updated, how to detect the changes so the entire app refreshes it?
Any suggestions greatly appreciated.
Upvotes: 1
Views: 276
Reputation: 2073
You can dump the application scope to see what's in it by creating a cfm and running:
dumpApplication.cfm:
<cfdump var="#application#">
Or, if your application scope is huge or something, you can target it specifically:
<cfdump var="#application.components.blog#">
Now, where is that cfset being run? If its being run in the Application.cfc in the OnApplicationStart method, then yes, it would only run once when the application first fires up and be cached after that. If you are constantly setting it somewhere, then it would fire that over and over again. You could change it to:
<cfif NOT structKeyExists(application.components,"blog")>
<cflock scope="application" type="exclusive" timeout="10">
<cfset application.components.blog ="#createObject('component','sharedcomponents.cfc.blog').init()#">
</cflock>
</cfif>
You don't have to cflock in the OnApplicationStart as it is singlethreaded.
Edited: As per commented advice on CFPARAM
Upvotes: 2
Reputation: 29870
You don't say where that code is or how / when it's executed, so it's impossible to confidently answer your question.
If it's in onApplicationStart() in Application.cfc, then - yeah - it'll only be executed when the application starts.
If, however, it's just in some file, it'll be re-executed every time that file is called, unless you take measures to ensure it's not called. EG: put a condition around it:
<cfif not (structKeyExists(application, "components") and structKeyExists(application.components, "blog"))>
<cfset application.components.blog = createObject('component','sharedcomponents.cfc.blog').init()>
</cfif>
You seem to be wondering if some magic takes place in that because your variable assignment sets something in the application scope that CF will automatically know not to rerun it if the file its in is reexecuted. No. CF just does what it's told: if you tell it to run that code, it will be run. Irrespective of which scope the variable is being assigned in.
You go on to ask about detecting if files have been updated and accordingly restart the whole application. How are these files being updated? I presume it's part of an active deployment process (eg: you do something to deploy them). If this is the case, aren't you in the best position to know when the application needs restarting? For one thing, not every code change is going to warrant an application restart, so you're not going to want to do it automatically, I should think?
You should have a script that runs applicationStop()
if you need to restart the app. Some people have their onRequestStart()
check for a URL param which then calls applicationStop()
if it's passed, but I personally think that's the wrong place to do it. For almost every request that condition will be irrelevant, so there's not point checking it every request for the one-in-a-million occasion that it's actually pertinent. I prefer having a separate script to do it, which is called as needs must.
Upvotes: 6