如何最好地创建和存储应用程序变量?

发布于 2024-12-18 19:40:51 字数 880 浏览 6 评论 0原文

我正在使用 ColdFusion 9.0.1

我正在接管一个站点,在我之前的人创建了大约 100 个变量并将它们放入 APPLICATION 范围。我相信他的 100 个变量随着每次页面加载而不断被覆盖。

基本上,他在 Application.cfc 中有这样的内容:

APPLICTION.VariableOne = "SomeStringOne";
APPLICTION.VariableTwo = "SomeStringTwo";
APPLICTION.VariableThree = "SomeStringThree";

我的计划是保持简单但又非常可读,以便测试应用程序范围中的特定结构。如果不存在,请创建结构和变量:

if (not isDefined("APPLICTION.AppInfo") or not isStruct(APPLICTION.AppInfo)) {
    APPLICTION.AppInfo = structNew();
    APPLICTION.AppInfo.VariableOne = "SomeStringOne";
    APPLICTION.AppInfo.VariableTwo = "SomeStringTwo";
    APPLICTION.AppInfo.VariableThree = "SomeStringThree";
}

当然,一旦站点上线并且我们完成了所有应用程序变量的创建,我会将其移至 onApplicationStart() 方法中。

我想要的解决方案必须更多地关注“可读性”,而不是“效率”。一些非 CFers,但非常有经验的程序员将使用它,并且需要快速“掌握”。

我的计划是否有漏洞或者效率太低?

是否有更易读的方式来创建和管理应用程序变量?

I am using ColdFusion 9.0.1

I am taking over a site and the guy before me created about 100 variables and put them into the APPLICATION scope. I believe that his 100 variables were continuously being overwritten with each page load.

Basically, he had this in Application.cfc:

APPLICTION.VariableOne = "SomeStringOne";
APPLICTION.VariableTwo = "SomeStringTwo";
APPLICTION.VariableThree = "SomeStringThree";

My plan is to keep it simple and yet very readable is to test for a specific structure in the application scope. If it's not there, create the structure and variables:

if (not isDefined("APPLICTION.AppInfo") or not isStruct(APPLICTION.AppInfo)) {
    APPLICTION.AppInfo = structNew();
    APPLICTION.AppInfo.VariableOne = "SomeStringOne";
    APPLICTION.AppInfo.VariableTwo = "SomeStringTwo";
    APPLICTION.AppInfo.VariableThree = "SomeStringThree";
}

Of course, once the site is live and we are done creating all of the application variables, I'd move this into the into the onApplicationStart() method.

The solution that I want has to be more about "readability" and less about "efficiency". Several non-CFers, but very experience coders will be using this and will need to "get it" quickly.

Does my plan have any gaping holes or is it too inefficient?

Is there a more readable way of creating and managing application variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霞映澄塘 2024-12-25 19:40:51

为什么不立即将定义移至 onApplicationStart() 中?如果您需要在开发过程中重置它们,您始终可以传入一个 URL 变量来标记其重置,如下所示:

<!--- in Application.cfc --->

<cffunction name="onRequestStart">
<cfif IsDefined("url.resetApp")>
  <cfset ApplicationStop()>
  <cfabort><!--- or, if you like, <cflocation url="index.cfm"> --->
</cfif>
</cffunction>

Why not move the definition into onApplicationStart() right now? If you need to reset them during development, you could always pass in a URL variable to flag it for reset, like so:

<!--- in Application.cfc --->

<cffunction name="onRequestStart">
<cfif IsDefined("url.resetApp")>
  <cfset ApplicationStop()>
  <cfabort><!--- or, if you like, <cflocation url="index.cfm"> --->
</cfif>
</cffunction>
酒浓于脸红 2024-12-25 19:40:51

实际上,在重新阅读OP并阅读建议的解决方案之后,我将不得不同意OP的设置,因为这个非常重要的原因:

这个,在onApplicationStart()中,

APPLICTION.AppInfo = structNew();
APPLICTION.AppInfo.VariableOne = "SomeStringOne";
APPLICTION.AppInfo.VariableTwo = "SomeStringTwo";

然后可以变成这个,然后,

<cflock name="tmp" type="readonly" timeout="15">
   <cfset REQUEST.AppInfo = APPLICATION.AppInfo />
</cflock>

您的应用程序可以继续方便地访问 REQUEST 变量,特别是,如果您决定要在同一范围内缓存 CFC,它们只需进入一个单独的键:

   APPLICATION.Model.MyObject = CreateObject('component','myobject');

当然,也会被注入进入请求(如果您选择)

想要走上面 Jake Feasel 的路线吗?没问题:

   <cfif isDefined('URL.reload')>
      <cfset APPLICATION.Model = StructNew() />
   </cfif>

现在您可以灵活地终止对象缓存,但保留变量(或者根据您的选择反之亦然)。

这是一个很好的设置,还有另一个原因:如果您想构建自己的开发/生产“模式”,其中开发模式始终重新编译 CFC,但生产模式会缓存它们。除此之外,您必须进行的唯一更改是上面提到的 REQUEST 设置:

<cfif (isProduction)>
    <cflock name="tmp" type="readonly" timeout="15">
       <cfset REQUEST.AppInfo = APPLICATION.AppInfo />
    </cflock>
<cfelse>
   <cfset REQUEST.AppInfo = StructNew() />
   <cfset REQUEST.AppInfo.VariableOne = "SomeStringOne" />
   ...etc...
</cfif>

您还可以将变量的设置和对象的创建放入 Application.cfc 中的私有方法中,以进一步方便。

Actually, after re-reading the OP, and reading the suggested solutions, I'm going to have to agree with the OP on his setup, for this very important reason:

This, in onApplicationStart()

APPLICTION.AppInfo = structNew();
APPLICTION.AppInfo.VariableOne = "SomeStringOne";
APPLICTION.AppInfo.VariableTwo = "SomeStringTwo";

Can then later be turned into this, within onRequestStart()

<cflock name="tmp" type="readonly" timeout="15">
   <cfset REQUEST.AppInfo = APPLICATION.AppInfo />
</cflock>

Your app can then go on to access the REQUEST vars conveniently, esp, if you decide you want to cache CFCs in the same scope--they would simply go into a separate key:

   APPLICATION.Model.MyObject = CreateObject('component','myobject');

Which, of course, also gets poured into REQUEST (if you choose)

Want to go Jake Feasel's route above? No problem:

   <cfif isDefined('URL.reload')>
      <cfset APPLICATION.Model = StructNew() />
   </cfif>

Now you're able to flexibly kill your object cache but maintain your vars (or vice versa as you choose).

This is a great setup for another reason: If you want to build in your own Development/Production "mode", in which the development mode always recompiles the CFCs, but the production mode keeps them cached. The only change you have to make on top of this, is the REQUEST set noted above:

<cfif (isProduction)>
    <cflock name="tmp" type="readonly" timeout="15">
       <cfset REQUEST.AppInfo = APPLICATION.AppInfo />
    </cflock>
<cfelse>
   <cfset REQUEST.AppInfo = StructNew() />
   <cfset REQUEST.AppInfo.VariableOne = "SomeStringOne" />
   ...etc...
</cfif>

You can also make the setting of vars and the creation of objects into a private method within Application.cfc, for even further convenience.

黯然#的苍凉 2024-12-25 19:40:51

我会继续使用 OnApplicationStart,但在 Application.cfc 之前的日子里,我们曾经做过类似 Application.Build 的事情,如果 Build 值不同,那么我们会对 Application 变量进行所有设置。如此快速和肮脏会是这样的:

<cfparam name="Application.Build" default="" />

<cfset Build = "28-Nov-2011" />

<cfif Application.Build IS NOT Variables.Build OR StructKeyExists(URL, "Rebuild")>
 <cfset Application.Build = Variables.Build />
 <!--- A bunch of other CFSETs --->
</cfif>

这个方法虽然是我们使用的东西,当时我们只有 Application.cfm

I would go ahead and just use OnApplicationStart but back in the pre Application.cfc days we used to do something like Application.Build and if the Build value was different then we did all of our sets on Application variables. So quick and dirty would be something like:

<cfparam name="Application.Build" default="" />

<cfset Build = "28-Nov-2011" />

<cfif Application.Build IS NOT Variables.Build OR StructKeyExists(URL, "Rebuild")>
 <cfset Application.Build = Variables.Build />
 <!--- A bunch of other CFSETs --->
</cfif>

This method though was something we used back when all we had was the Application.cfm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文