2012-11-06 75 views
5

我想通過自己緩存視圖結果對象來重新創建MVC 4中的OutputCache操作篩選器的大部分功能。我不想使用OutputCache操作過濾器的原因是因爲我不能將它與AppFabric和部分視圖一起使用;部分視圖總是存儲在MemoryCache中,我希望跨服務器場使用緩存對象。在AppFabric中手動緩存MVC視圖

的第一個問題我已經是

{"Type 'System.Web.Mvc.TempDataDictionary' cannot be serialized. 
Consider marking it with the DataContractAttribute attribute, and marking all of 
its members you want serialized with the DataMemberAttribute attribute. 
If the type is a collection, consider marking it with the 
CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for 
other supported types."} 

這使我懷疑我是否應該緩存別的東西返回的內容基本上是在年底的看法。有沒有人知道我應該緩存什麼,而是重新創建視圖或用於緩存服務器場上部分視圖的不同方法?我不想爲此使用第三方插件。

感謝

更新:我開始緩存局部視圖,像這樣的字符串表示:

using (StringWriter sw = new StringWriter()) 
     { 
      ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "ViewName"); 
      ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); 
      viewResult.View.Render(viewContext, sw); 
      view = sw.GetStringBuilder().ToString(); 
     } 

這使得它很容易只是檢索緩存中的字符串,並返回其作爲內容行動。我仍在尋找其他建議或更好的方法來做到這一點。

回答

0

也許有點晚了,但讓我分享一下我的經驗吧。

雖然ASP.NET MVC構建在ASP.NET框架之上,但它有一些非常顯着的差異,使得在MVC中重用ASP.NET特性非常困難。你是真的:整頁輸出緩存和部分頁面輸出緩存是以完全不同的方式實現的。來自Greg Roberts的另一個blog post表明MVC中的Output有許多問題。它在WebForms中非常有用!

這就是爲什麼我把自己變成MvcDonutCaching(Nuget)。它解決了我們許多問題。請閱讀引言herecodeplex

對你來說好消息是MvcDonutCaching也完全兼容AppFabric Caching; DevTrends幾個月前發佈了一個article。這意味着您可以使用新的輸出緩存提供程序(包含在AppFabric 1.1中)。

添加這個新提供者非常簡單,就像添加一個引用並以這種方式更改配置一樣。

<caching> 
    <outputCache defaultProvider="DistributedCache"> 
    <providers> 
     <add name="DistributedCache" 
      type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" 
      cacheName="default" 
      dataCacheClientName="default" /> 
    </providers> 
    </outputCache> 
</caching> 

希望這會幫助你,因爲它可以幫助我們很多!