在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, 還有什么好的方式請指教,
