سؤال

هل يمكن لأي شخص أن يجد طريقة لتحسين هذا الرمز قليلاً؟أرغب في قراءة ملف 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) الحل هو محاولة النقي جافا. انظر هذا الموضوع SO للجافا النقي أمثلة .

وP.S. راجع للشغل، في حالة أداء خاص يحتاج يجب عليك أن تنظر باستخدام تخزين آخر لتكوين. الاستعلام SELECT بسيط إلى الخلية حسن البالغ من العمر يمكن أن يكون أسرع بكثير لمجموعات البيانات الكبيرة.

نصائح أخرى

للتوسع في تعليق رايبر، قد تفكر في استخدام هذا الأسلوب بدلاً من ذلك.أفترض أنك تستخدم CF8.01 أو إصدار أحدث، حيث أنني أستفيد من تدوين البنية الضمنية المتداخلة.يمكن تحويل هذا بسهولة إلى بناء جملة CF7/6/etc، لكنه لن يكون واضحًا أو موجزًا.

مرة أخرى، ينطبق هذا فقط إذا لم يتم استخدام ملف ini الخاص بك بواسطة أي تطبيقات أو أشخاص آخرين، ولا يتم استخدامه يحتاج أن تكون بصيغة ini.

إعدادات.cfm:

<cfset variables.settings = {
    fooSection = {
        fooKey = 'fooVal',
        fooNumber = 2,
    },
    fooSection2 = {
        //...
    },
    fooSection3 = {
        //...
    }
} />

تطبيق.cfc: (طريقة onApplicationStart فقط)

<cffunction name="onApplicationStart">
    <cfinclude template="settings.cfm" />
    <cfset application.workflow = variables.settings />
    <cfreturn true />
</cffunction>

بالإضافة إلى ذلك، لقد استخدمت CFEncode تطبيق لتشفير محتويات settings.cfm.لن يحميك من شخص يحصل على نسخة من الملف ويريد معرفة محتوياته المشفرة (التشفير ليس بهذه القوة، وهناك طرق لرؤية المحتويات دون فك تشفيرها)، ولكن إذا كنت فقط إذا كنت ترغب في إبعاد بعض الأشخاص الفضوليين، فهذا يضيف حاجزًا إضافيًا صغيرًا أمام الدخول قد يردع بعض الأشخاص.


تحديث: نظرًا لأنك تركت للتو تعليقًا يفيد بأنك تستخدم CF7، فإليك بناء جملة CF7 الأصلي:

إعدادات.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. كما أنها تحافظ اختياريا هيكل شقة أو إنشاء هياكل فرعية على أساس [الأقسام] في ملف 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