有人能找到一种方法来改进一下这段代码吗?我想一下子读入一个INI文件,并创建一个相应的数据结构。
<cfset INIfile = expandPath(".") & "\jobs.ini">
<cfset profile = GetProfileSections(INIfile)>
<cfloop collection="#profile#" item="section">
<cfloop list="#profile[section]#" index="entry">
<cfset app.workflow[section][entry]=GetProfileString(INIfile, section, entry) >
</cfloop>
</cfloop>发布于 2009-10-28 16:53:09
我不相信你可以使用CFML power来改善这一点。你需要解析巨大的ini文件吗?如果不是,为什么你想要改进你的代码,这对我来说很简单。
其他可能的解决方案(虽然对于CF很常见)是尝试纯Java。请参阅此SO线程以了解纯Java examples。
附注:顺便说一句,如果您有特殊的性能需求,您应该考虑使用其他存储进行配置。对于大型数据集,对旧的好的MySQL进行简单的SELECT查询可能要快得多。
发布于 2009-10-29 00:09:57
要扩展ryber的评论,您可以考虑使用这种方法。我假设您使用的是CF8.01或更高版本,因为我使用了嵌套的隐式结构表示法。这可以很容易地转换为CF7/6/etc语法,但不会那么干净或简洁。
同样,这仅适用于您的ini文件未被任何其他应用程序或人员使用的情况,并且不需要为ini格式。
settings.cfm:
<cfset variables.settings = {
fooSection = {
fooKey = 'fooVal',
fooNumber = 2,
},
fooSection2 = {
//...
},
fooSection3 = {
//...
}
} />Application.cfc: (仅限onApplicationStart方法)
<cffunction name="onApplicationStart">
<cfinclude template="settings.cfm" />
<cfset application.workflow = variables.settings />
<cfreturn true />
</cffunction>此外,我还使用了CFEncode应用程序对settings.cfm的内容进行加密。它不会保护你免受那些想要查看其加密内容的人的影响(加密并不是那么强,有办法在不解密的情况下查看内容),但是如果你只是想把一些爱管闲事的人拒之门外,它会增加一些额外的进入障碍,这可能会阻止一些人进入。
更新:因为你刚刚留下了一个评论,说你在CF7上,所以这里是原生的CF7语法:
settings.cfm:
<cfset variables.settings = StructNew() />
<cfset variables.settings.fooSection = StructNew() />
<cfset variables.settings.fooSection.fooKey = 'fooVal' />
<cfset variables.settings.fooSection.fooNumber = 2 />
<!--- ... --->或者,您可以使用JSONUtil和CFSaveContent继续使用类似于JSON的方法(类似于我最初的语法),但使用的是CF7:
<cfsavecontent variable="variables.jsonSettings">
{
fooSection = {
fooKey = 'fooVal',
fooNumber = 2,
},
fooSection2 = {
//...
},
fooSection3 = {
//...
}
};
</cfsavecontent>
<cfset variables.settings = jsonUtil.deserializeFromJSON(variables.jsonSettings) />发布于 2009-12-18 22:53:49
我创建了一个在许多应用程序中使用的CFC。当你初始化它的时候,你给它一个ini文件路径,它就会基于这个ini文件创建一个结构。它还可以选择使结构保持平坦或基于ini文件中的节创建子结构。然后,您可以使用它的getSetting()方法获取单个方法,也可以使用getAllSettings()返回整个结构。你可能会发现它很有帮助。
<cfcomponent hint="converts INI file to a structure">
<cfset variables.settings=structNew() />
<cffunction name="init" access="public" output="false" returntype="any">
<cfargument name="configurationFile" type="string" required="yes" />
<cfargument name="useSections" default="false" type="boolean" />
<cfset var local=structNew() />
<cfif fileExists(arguments.configurationFile)>
<!--- Get the [sections] in the .INI file --->
<cfset local.sectionStruct=getProfileSections(arguments.configurationFile) />
<!--- Loop over each of these sections in turn --->
<cfloop collection="#local.sectionStruct#" item="local.item">
<cfset local.workingStruct=structNew() />
<cfloop list="#local.sectionStruct[local.item]#" index="local.key">
<!--- Loop over the keys in the current section and add the key/value to a temporary structure --->
<cfset local.workingStruct[local.key]=getProfileString(arguments.configurationFile,local.item,local.key) />
</cfloop>
<cfif arguments.useSections>
<!--- Copy the temporary structure to a key in the setting structure for the current section --->
<cfset variables.settings[local.item]=duplicate(local.workingStruct) />
<cfelse>
<!--- Append the temporary structure to the setting structure --->
<cfset structAppend(variables.settings,local.workingStruct,"yes") />
</cfif>
</cfloop>
<cfelse>
<cfthrow
message="Configuration file not found. Must use fully-qualified path."
extendedinfo="#arguments.configurationFile#"
/>
</cfif>
<cfreturn this>
</cffunction>
<cffunction name="getAllSettings" access="public" output="false" returntype="struct">
<cfreturn variables.settings>
</cffunction>
<cffunction name="getSetting" access="public" output="false" returntype="string">
<cfargument name="settingName" required="yes" type="string" />
<cfset var returnValue="" />
<cfif structKeyExists(variables.settings,arguments.settingName)>
<cfset returnValue=variables.settings[arguments.settingName] />
<cfelse>
<cfthrow
message="No such setting '#arguments.settingName#'."
/>
</cfif>
<cfreturn returnValue>
</cffunction>
</cfcomponent>https://stackoverflow.com/questions/1633097
复制相似问题