在mvc开发中本人经常会遇到这样的问题,在action中返回列表的时候经常会遇到有缓存,但是那都是浏览器的自带的缓存,没有在mvc里面真正使用过,我们经常在action里面用
Response.Cache.SetCacheability(HttpCacheability.NoCache); //清除缓存
或者是在页面添加清除缓存标签
this.ControllerContext.HttpContext.Response.AddHeader("cache-control", "no-cache"); //清除缓存
来清除缓存,但是为了优化系统必须有,为了提升,今天看了下,首先看了下大神老赵的博客关于缓存的方案,大牛就是大牛,非常经典http://www.cnblogs.com/JeffreyZhao/archive/2009/09/17/aspnet-mvc-fragment-cache-1.html
还有一种就是mvc的输出缓存OutputCacheAttribute 相当简单,这个跟asp.net中@ OutputCache 属性是一样的,唯一不受 OutputCacheAttribute 支持的 @ OutputCache 属性是 VaryByControl。
[OutputCache(Duration = 10)] public ActionResult Index() { return View(DateTime.Now); }
但是在项目中一般建议不要这么写,因为这样如果控制器多的话比较难以控制,我觉得通过来规定整个控制器也是可取的
[OutputCache(Duration = 10)] public class HomeController : Controller { // // GET: /Default1/ public ActionResult Index() { return View(DateTime.Now); } }
但是网上还是建议通过xml形式来规定,这样更容易的全局控制
[OutputCache(CacheProfile = "objectboy")] public ActionResult Index()
Web.Config格式如下:
<system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="objectboy" duration="10" varyByParam="*" /> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web>
几种方式都将在页面缓存10s, 还有什么好的方式请指教,