|
|
Hi,
I have an CFC that I set in application scope in the onApplicationStart method
which contains some system parameters, one of which is a flag saying whether
the site is currently "Online". I call a method on this CFC in the
Application.cfc "onRequest" method to see if the flag value has changed.
This method looks up a parameter set in the database that says whether the
site is "online" or "offline". If the site is currently online, I want to only
redo the query every 5 mins. However if the site is offline, I want to check
more frequently (every 5 secs) as to whether the flag has changed back.
What I am not sure of, is whether I need to be using <cflock> when I change
from "online" to "offline"? There is only a single instance of this object,
but what happens if multiple requests are calling the "isSiteOnline" method
simultaneously? Is this "safe"?
Some code snippets are attached to illustrate.
Any advice would be greatly appreciated.
Regards,
Andrew.
In Application.cfc:
<cffunction name="onApplicationStart">
....
<cfset application.systemOptions = createObject("component",
"system.SystemOptions").init()>
....
</cffunction>
<cffunction name="onRequest">
<cfset isOnline = application.systemOptions.isSiteOnline()>
</cffunction>
The SystemOptions CFC stores the current status in an instance variable.
eg.
SystemOptions.cfc
<cfcomponent name="SystemOptions" output="false">
<cfset variables.hrxmlDatasource = "testdb">
<cffunction name="init" access="public" returntype="SystemOptions"
output="false" hint="Create a new instance of SystemOptions">
<cfset variables.instance = STRUCTNEW() />
<cfset this.setDbOnlineCacheInterval()>
<cfreturn this />
</cffunction>
<cffunction name="isSiteOnline" access="public" output="false"
returntype="boolean">
<cfset var siteOffline = false />
<cfquery name="qIsOffline" datasource="#variables.hrxmlDatasource#"
Cachedwithin="#CreateTimeSpan(0, 0, 0, variables.instance.cacheInterval)#">
..... // query that returns a single row if the site is to be taken
offline
</cfquery>
<cfif qIsOffline.RecordCount gt 0>
<cfset siteOffline = true>
<cfset this.setDbOfflineCacheInterval()>
<cfelse>
<cfset this.setDbOnlineCacheInterval()>
</cfif>
<cfreturn siteOffline/>
</cffunction>
<cffunction name="setDbOnlineCacheInterval" access="public" output="false"
returntype="void">
<!--- DO I NEED TO USE CFLOCK HERE???? --->
<cfset variables.instance.cacheInterval = 300 /> <!--- When online check
for a change every 5 mins --->
</cffunction>
<cffunction name="setDbOfflineCacheInterval" access="public" output="false"
returntype="void">
<!--- AND DO I NEED TO USE CFLOCK HERE???? --->
<cfset variables.instance.cacheInterval = 5 /> <!--- When offline checks
for a change every 5 secs --->
</cffunction>
</cfcomponent>
|
|