coldfusion - onApplicationStart 在 ColdFusion 中是个好主意吗?

标签 coldfusion scope restart

我必须在 ColdFusion 中使用变量(查询结果集) ,它将从其他应用程序数据库中获取结果,并存储在 Coldfusion 应用程序中。

主要思想是我只需要在服务器启动时调用其他应用程序数据库并将结果缓存在本地。我需要在我的应用程序的其他页面中读取变量。我不会在任何页面中覆盖该变量。

在谷歌搜索中,我发现“onApplicationStart”对于在应用程序启动时分配变量很有用。

使用 onApplicationStart 好还是有其他方法?我们可以在启动时(一次)分配一个变量。

如果onApplicationStart没问题:如何使用?也许任何解释清楚的链接都有帮助。

最佳答案

好吧,这取决于。此查询数据多久更新一次?如果它真的是不变的,那么 onApplicationStart() 是放置它的好地方。但是,如果它经常更改,您可以告诉 Coldfusion 到 cache the query对于a certain period of time ,那么你就不需要搞乱 onApplicationStart(),而是当你调用查询时它会自动返回缓存的结果(在你指定的时间段内)。

无论如何,我会编写一个自定义函数来检索数据。然后从 onApplicationStart() 或其他地方调用它就很简单了。

Startup.cfc:(随便命名)

<!--- Replace the datasource name with your db name --->
<cffunction name="getStartupQuery" hint="Returns a query recordset for startup">
    <cfargument name="datasource" required="no" type="string" default="OtherAppDB">
    <!--- Init the query variable --->
    <cfset var result = queryNew("id")>

    <!-- Get the query dataset --->
    <cfquery name="result" datasource="#arguments.datasource#">
         YOUR QUERY HERE
    </cfquery>

    <cfreturn result>
</cffunction>

Application.cfc: (只是重要的部分)

<cffunction name="onApplicationStart">
    <!--- init the startup.cfc, then retrieve the data
    and save it to the application scope. Remember the component name must match
    your component above --->
    <cfset var startup = createObject("component", "startup")>
    <cfset application.varFromOtherDB = startup.getStartupQuery()>
    <cfreturn true>
</cffunction>

现在,您应该能够使用以下方法从应用程序中的任何 CFM 或​​ CFC 访问此变量:

<cfset myNewVar = application.varFromOtherDB>
or
#application.varFromOtherDB#

如果您使用 onApplicationStart() 方法,我强烈建议您实现一种方法来重新初始化应用程序。例如,see this other discussion .

关于coldfusion - onApplicationStart 在 ColdFusion 中是个好主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1934681/

相关文章:

macos - 如何在 OS X 上重新启动 nginx

SQL 文件流和 Coldfusion。将非常大的文件读入变量

python - if __name__ == __main__ 的范围

$.getJSON 调用中的 JavaScript 变量作用域

javascript Angularjs 从数组中获取单个值

c - 如何通过关闭 C 中的所有文件描述符来重新启动或启动进程

iphone - 如果应用程序意外关闭,如何重新启动应用程序

从版本 11 升级到 ColdFusion 2018 后出现 ORM 问题

asp.net-mvc - MVC : I need to understand the Model

coldfusion - 重置 ColdFusion session 变量(CFTOKEN、CFID、JSESSIONID)的最有效方法是什么?