首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以解析ColdFusion 9.0.1缓存内存的内容吗?

可以解析ColdFusion 9.0.1缓存内存的内容吗?
EN

Stack Overflow用户
提问于 2012-03-16 23:37:36
回答 1查看 274关注 0票数 2

我一直对从CF8到9的变化视而不见,在这种情况下,如果不创建自定义缓存或其他不值得付出努力的激烈解决方案,就不再可能写入磁盘缓存。

此时,我已经放弃了尝试将应用程序转换为(如果可能的话)将缓存文件的内容写入静态cfm文件的相同方法。我现在更好奇了,因为我已经深入地研究了它。我在找一个比我自己更有经验的人。

我想了解或知道如何使用模板缓存:

  1. 能够锁定默认或自定义缓存中的模板,并在不清除整个缓存的情况下对其进行刷新。
  2. 出于好奇或调试方法,查看或解析特定缓存模板的内容。

这是我所使用的代码,需要Cf9.0.1,因为我相信由于EhCache 2.0的添加,有些缓存内存函数在9.0中不可用。

代码语言:javascript
复制
<cftry>
        <cfcache action='serverCache' timeout='#CreateTimeSpan(0,0,0,10)#' stripwhitespace='true' 
                 usequerystring='true'>
            Stuff to cache.
            <br/>
            <cfoutput>#now()#</cfoutput>
            <br/>
        </cfcache>

        <!--- Get the cached contents --->
        <cfdump var="#cacheGetProperties()#">
        <cfdump var="#getAllTemplateCacheIds()#">
        <!---Locate a single cached item --->
        <cfscript>
            cacheArray = getAllTemplateCacheIds();
            WriteOutput("Before<br/>");
            for(i=1;i LTE ArrayLen(cacheArray);i=i+1)
            {
                writeOutput(cacheArray[i] & "<br/>");
                if(FindNoCase(scriptPath, cacheArray[i]))
                {
                    //expect only to find min and max one per string so no need to worry about out of bounds
                    cacheIDSubStr =  REFind("[a-fA-F\d]{32}(?=_LINE:\d*$)",cacheArray[i],1,1);
                    cacheID = Mid(cacheArray[i],CacheIDSubStr.pos[1],CacheIDSubStr.len[1]);
                   //Failure to delete cache expected fireworks
                    //WriteOutput(cacheID&"<br/>");
                    //cacheObject = CacheGet(cacheID);
                    //CacheRemove(cacheID);
                    templateCache = cacheGetSession("template");
                      //Tooling around with the exposed guts of cacheGetSession
                    WriteDump(templateCache.getKeys());
                }
            }
        </cfscript>
    <cfcatch type="Any">

        <cfscript>
            writeoutput("Error:" & cfcatch.message);
        </cfscript>

    </cfcatch>
    </cftry>

不应该存在错误,但是它是从原始编辑到这里发布的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-21 03:24:22

秘密在于CF9.0.1函数cacheGetSession()中,以及对其他函数的修改,以key作为参数。

下面的代码只适用于9.0.1.

假设:

代码语言:javascript
复制
<cfcache action="serverCache" timeout="#CreateTimeSpan(0,0,0,10)#" stripwhitespace="true" usequerystring="true">
Stuff to cache in the default store.
<br/>
<cfoutput>#Now()#</cfoutput>
</cfcache>

<cfcache action="serverCache" key="myCustomCache" timeout="#CreateTimeSpan(0,0,0,10)#" stripwhitespace="true" usequerystring="true">
Stuff to cache in the 'myCustomCache' store.
<cfoutput>#Now()#</cfoutput>
</cfcache>

分别访问缓存的标识符

默认(模板):

代码语言:javascript
复制
<cfdump var=#getAllTemplateCacheIds()#>

myCustomCache:

代码语言:javascript
复制
<cfdump var=#cacheGetSession('myCustomCache',true).getKeys()#>

读取自定义缓存的数据

代码语言:javascript
复制
<cfset keys = cacheGetSession('myCustomCache',true).getKeys() />

<cfcache key="myCustomCache" action="get" name="dataInCache" id="#keys[1]#">

<cfdump var=#dataInCache#>

独立于默认刷新自定义缓存

代码语言:javascript
复制
<cfset cacheRemove(keys[1],true,'myCustomCache')>

遗憾的是,文档在9.0.1中潜入的功能方面还不是最好的。幸运的是,CF用户基础擅长挖掘这些东西,即罗布-布鲁克斯·比尔森,他有很多关于CF和缓存的优秀文章。

在以这种方式使用缓存时,您应该记住几个警告

  1. 这些调用绑定到引擎盖下的EhCache,即Java,如果您试图访问不存在的密钥,则可能返回NULL。将结果包装在IsNull()中以进行验证。
  2. server-wide,()具有误导性(正如Rob在他的博客中指出的那样),您可能认为您正在检索应用程序中与会话相关的缓存数据,但事实并非如此--这是server-wide,,您将在共享应用程序环境中看到其他缓存的密钥。

所以,要小心冲洗适当的数据..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9745847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档