我今天早上在将一些文件部署到 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(),它就会抛出异常:
"The value returned from the init function is not of type MyObject."
此问题未发生在部署此更改的任何其他环境中 - 仅发生在生产环境中。
唯一修复它的方法是清除 ColdFusion Administrator 中的模板缓存。
因此,我正在寻找一种方法来防止这种情况在未来发生,和/或一种在我部署文件时自动清除模板缓存的方法。
仅供引用,我目前使用 Tortoise SVN 部署文件。
最佳答案
在您的 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();
关于caching - 如何在部署过程中清除 ColdFusion 模板缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10620808/