3

今天早上我在将一些文件部署到 ColdFusion 网站/应用程序后遇到了一个问题。

我用一些新代码更新了现有的 CFC。CFC 有一个返回实例化对象的 init() 方法:

原始 MyObject.cfc:

<cfscript>
    VARIABLES.MyParam = "";
</cfscript>

<cffunction name="init" returntype="MyObject" output="false">
    <cfargument name="MyParam" type="String" required="true" />

    <cfscript>
        VARIABLES.MyParam = ARGUMENTS.MyParam;

        return THIS;
    </cfscript>
</cffunction>

新的 MyObject.cfc:

<cfscript>
    VARIABLES.MyParam = "";
</cfscript>

<cffunction name="init" returntype="MyObject" output="false">
    <cfargument name="MyParam" type="String" required="true" />

    <cfscript>
        setMyParam(ARGUMENTS.MyParam);

        return THIS;
    </cfscript>
</cffunction>

<cffunction name="setMyParam" output="false" returntype="Void">
    <cfargument name="MyParam" type="String" required="true" />

    <cfset VARIABLES.MyParam = Trim(ARGUMENTS.MyParam) />
</cffunction>

<cffunction name="getMyParam" output="false" returntype="String">
    <cfreturn VARIABLES.MyParam />
</cffunction>

每当扩展此 CFC 的对象调用 init() 时,它都会引发异常:

“从 init 函数返回的值不是 MyObject 类型。”

此问题在部署此更改的任何其他环境中均未发生 - 仅在生产中。

唯一修复它的是清除 ColdFusion Administrator 中的模板缓存。

所以,我要么在寻找一种方法来防止这种情况在未来发生,要么在我部署文件时自动清除模板缓存。

仅供参考,我目前使用 Tortoise SVN 部署文件。

4

1 回答 1

4

在您的 init() 中(或更优选地,在另一个重新加载样式的方法中),以编程方式调用 Admin API 的 clearTrustedCache() 方法:

<cfscript>

     // Login is always required (if the administrator password 
     // is enabled in the ColdFusion Administrator). 
     // This example uses two lines of code. 

     adminObj = createObject("component","cfide.adminapi.administrator");
     adminObj.login("admin");

     // Instantiate the runtime object. 
     myObj = createObject("component","cfide.adminapi.runtime");

     // clear cache 
     myObj.clearTrustedCache();

     // Stop and restart trusted cache. However, only the clearTrustedCache function needs to be called.
     myObj.setCacheProperty("TrustedCache", 0);
     myObj.setCacheProperty("TrustedCache", 1);
</cfscript>

这个功能早在 CF7 ( Source ) 就已经存在。请注意,您需要为此提供 CF 管理员密码。

如果您在管理员中启用了该选项,我还建议您清除组件缓存:

    myObj.clearComponentCache();
于 2012-05-16T14:52:24.893 回答