문제

누구 든지이 코드를 조금 개선하는 방법을 찾을 수 있습니까? 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>
도움이 되었습니까?

해결책

나는 당신이 CFML 전원을 사용하여 이것을 개선 할 수 있다고 생각하지 않습니다. 거대한 INI 파일을 구문 분석해야합니까? 그렇지 않다면 왜 코드를 개선하고 싶을까요?

다른 가능한 (CF에는 일반적이지만) 솔루션은 순수한 Java를 시도하는 것입니다. 순수한 자바의 스레드를 참조하십시오 .

PS BTW, 특수 성능 요구의 경우 구성을 위해 다른 스토리지를 사용하는 것을 고려해야합니다. 오래된 Good MySQL에 대한 간단한 선택 쿼리는 대형 데이터 세트의 경우 훨씬 빠를 수 있습니다.

다른 팁

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) />

많은 앱에서 사용하는 CFC를 만들었습니다. INI FilePath를 시작할 때 INI FilePath를 제공하고 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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top