1

我正在编写一个使用 ini 文件来存储所有状态代码(错误、成功代码等)的应用程序。一个非常简单的版本是这样的:

[success]
000=Status code not found.

[error]
000=Error code not found.
001=Username/Password not found.

我的 CF 组件使用以下代码:

component  hint="Set of tools to interact with status codes."{
    public function init(string codefile) any{
        this.codefile = Arguments.codefile;
    }

    public function getCodeString(string type, string code) string{
        var code = getProfileString(Variables.codefile, Arguments.type, Arguments.code);
        return code;
    }
}

当我调用 getProfileString 时,我假设 Railo 打开文件,搜索键并返回值。所以随着我的应用程序的增长和我有更多的代码,我希望这个过程会减慢。那么有没有一种方法可以在我的 init 方法中打开文件并将其全部读入变量范围,然后从那里调用 getProfileString ?

4

2 回答 2

3

如果您想坚持使用 .ini 方法,您甚至可以在 onApplicationStart 中解析您的 ini 文件并将数据推送到应用程序范围,就像 @Sergii 为 XML 文件推荐的那样。

做这样的事情:

var sections = getProfileSections(variables.codeFile);
var sectionEntries = [];
var indx = 0;
for (key in sections){
    sectionEntries = listToArray(sections[key]);
    application[key] = {};
    for (indx=1; indx <= arraylen(sectionEntries); indx++){
            application[key][sectionEntries[indx]] = getProfileString(variables.cfgFile,key,sectionEntries[indx]);
    }
}

还没有在 Railo 上测试过,但它至少应该在 ColdFusion 9 上工作

于 2011-04-09T05:37:52.813 回答
1

因为您使用的是 Railo,所以可能有最简单的解决方案:将文件放入 RAM 文件系统。

所以文件的完整路径看起来像ram:///some/path/to/config.ini.

显然,您需要先将文件写入 RAM,可能是在第一次请求时。

因此,组件的略微修改版本可能看起来像这样:

component  hint="Set of tools to interact with status codes."{

    public function init(string codefile, string.ramfile) any{
        variables.codefile = arguments.codefile;
        variables.ramfile = arguments.ramfile;
    }

    public function getCodeString(string type, string code) string{

        if (NOT fileExists(variables.ramfile)) {
            fileCopy(variables.codefile, variables.ramfile);
        }

        return getProfileString(variables.ramfile, arguments.type, arguments.code);

    }
}

请注意,我已更改this.codefilevariables.codefile.init

无论如何,我也不确定 ini 文件是最方便和可维护的解决方案。您每次都需要以任何方式解析它,对吗?如果您需要文件配置,请使用 XML。只需解析它onApplicationStart并将数据推送到应用程序范围内。

于 2011-04-08T20:44:29.213 回答