2012-03-16 63 views
2

我從CF8到9的變化一直都是盲目的,在這種情況下,如果不創建自定義緩存或其他一些不值得花費的艱鉅解決方法,寫入磁盤緩存將不再可能。是否可以解析ColdFusion 9.0.1緩存內存的內容?

在這個時候,我已經放棄了嘗試轉換應用程序以支持,如果可能的話,將緩存文件內容寫入靜態cfm文件的方法。我現在更加好奇,因爲我已經深入瞭解了它。我在找一個比我自己有更多經驗的人。

我想什麼,瞭解或知道如何與模板緩存做的是:

  1. 能夠針對違約或自定義緩存模板,它不清除整個緩存刷新。
  2. 出於好奇或調試方法查看或解析特定緩存模板的內容。

這是我正在使用的代碼,需要CF 9.0.1,由於一些緩存功能在9.0中不可用,這是因爲我相信EhCache 2.0的增加。

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

沒有錯誤應該存在,但它被編輯從原來在這裏發佈。

回答

1

祕密是在CF 9.0.1功能cacheGetSession(),以及修改其他功能,以key作爲參數。

以下代碼僅適用於9.0.1。

假設:

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

訪問高速緩存分開

默認(模板)的標識符:

<cfdump var=#getAllTemplateCacheIds()#> 

myCustomCache:

<cfdump var=#cacheGetSession('myCustomCache',true).getKeys()#> 

讀自定義緩存

<cfset keys = cacheGetSession('myCustomCache',true).getKeys() /> 

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

<cfdump var=#dataInCache#> 

的數據獨立地衝洗默認

<cfset cacheRemove(keys[1],true,'myCustomCache')> 

可悲的自定義緩存,文檔不是處於最佳狀態與問候的功能,用9.0.1進入。幸運的是,CF用戶羣擅長挖掘這些東西,即Rob-Brooks Bilson,他們在CF和緩存方面擁有許多優秀的文章。

夫婦的警告你應該以這種方式與緩存的工作要記住:

  1. 這些電話聯繫到了Ehcache引擎蓋,這是Java的下,如果你嘗試訪問可能返回NULL值不存在的鍵。將結果包裹在IsNull()以驗證。

  2. cacheGetSession()被誤導(如羅布指出,在他的博客),你可能會認爲您檢索會話相關的緩存數據,因爲它適用於您的應用程序,但事實並非如此 - 它是服務器範圍內的,您將在共享應用程序環境中看到其他緩存的密鑰。

所以,照顧到沖洗相應的數據 ...