mvcdonutcaching
King Kong
ASP.NET MVC可擴展的甜甜圈緩存將甜甜圈緩存帶到ASP.NET MVC 3及以後。該代碼使您可以緩存所有頁面,除了一個或多個html.actions,可以執行每個請求。非常適合用戶特定內容。
將甜甜圈緩存添加到您的MVC項目中的最佳方法是使用Nuget軟件包。在Visual Studio中,選擇工具|庫軟件包管理器,然後選擇“軟件包管理器”控制台或管理Nuget軟件包。通過控制台,只需鍵入安裝包裝mvcdonutcaching即可擊中返回。從GUI中,只需搜索MVCDonutCaching ,然後單擊“安裝”按鈕。
該軟件包在內置的HTML.Action HTML助手中添加了幾個過載。每個過載中的額外參數被命名為“排除FromParentCache” 。將其設置為true,以適用於不應被緩存的任何動作,或者應與頁面其餘部分具有不同的緩存持續時間。
@Html . Action ( "Login" , "Account" , true )該軟件包還包括用於代替內置outputcacheattribute的Donutoutputcacheattribute。該屬性通常放置在需要緩存的每個控制器操作上。
您可以指定固定持續時間:
[ DonutOutputCache ( Duration = "300" ) ]
public ActionResult Index ( )
{
return View ( ) ;
}或者,使用緩存配置文件:
[ DonutOutputCache ( CacheProfile = "FiveMins" ) ]
public ActionResult Index ( )
{
return View ( ) ;
}如果您使用的是緩存配置文件,請確保在web.config中配置配置文件。在系統中添加以下內容。 web元素:
< caching >
< outputCacheSettings >
< outputCacheProfiles >
< add name = " FiveMins " duration = " 300 " varyByParam = " * " />
</ outputCacheProfiles >
</ outputCacheSettings >
</ caching >您還可以配置輸出緩存以使用自定義提供商:
< caching >
< outputCache defaultProvider = " DistributedCacheProvider " >
< providers >
< add name = " DistributedCacheProvider " type = " DevTrends.Example.DistributedCacheProvider " />
</ providers >
</ outputCache >
</ caching >請注意,該項目不包含自定義提供商,但是您可以通過subclassing system.web.caching.outputcacheprovider輕鬆編寫一個自定義提供商。網絡上也有許多實現。
現在可以在DevTrends博客上獲得MVC可擴展甜甜圈緩存的綜合指南。