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可扩展甜甜圈缓存的综合指南。