ASP NET MVC OutputCache

ASP.NET MVC 提供了一个Filter来实现缓存,如果这个Attribute在方法上,当前方法的输出会被缓存起来,如果Attribute在Controller上,控制器中所有的方法的输出都会被缓存起来。这里的缓存可以设置过期时间,并且可以设置输出策略等等。

1.OutputCache 简单Demo

[OutputCache(Duration = 60)]
public ActionResult Index()
{
      ViewBag.date = DateTime.Now.ToString();
      return View();
}
输出页面

<br />
@ViewBag.date
效果:

2015/6/10 10:40:08
总结:缓存过期时间设置为60秒,在60秒内刷新页面输出缓存页面.

删除缓存:

public ActionResult RemoveCache()
{
     var url = Url.Action("Index", "Home");
     HttpResponse.RemoveOutputCacheItem(url);
     return Content(string.Format("Clear Output Cache by Url {0} Success!", url));
}
当执行RemoveCache方法后,/Home/Index方法输出的缓存就会被清除。

2.带参数的缓存

[OutputCache(Duration = 60, VaryByParam = "id")]
public ActionResult Index2(int id)
{
      ViewBag.date = DateTime.Now.ToString();
      ViewBag.post = id;
      return View();
}
当我们访问

http://localhost:2065/Home/Index2/1

输出:

Index2
2015/6/10 10:45:19 
1
我们刷新继续访问,输出结果不变。那么这时候我们如何删除带参数的缓存呢?参照如下方法:

public ActionResult RemoveCacheById(int id)
{
      var url = Url.Action("Index2", "Home", new { id = id });
      HttpResponse.RemoveOutputCacheItem(url);
      return Content(string.Format("Clear Output Cache by Url {0} Success!", url));
}
我们访问:

http://localhost:2065/Home/RemoveCacheById/1

的时候,id=1的输出缓存将会被清除。

3.多个参数的缓存

[OutputCache(Duration = 3600, VaryByParam = "author;postname")]
public ActionResult Blog(string author, string postname)
{
     this.ViewBag.Author = author;
     this.ViewBag.PostName = postname;
     return View();
}

public ActionResult RemoveBlogCache(string author, string postname)
{
      Outputcache root
     var url = Url.Action("Blog", "Home", new { author = author, postname = postname });

      Clean output cache by root
     HttpResponse.RemoveOutputCacheItem(url);
     return Content(string.Format("Clear Output Cache by Url {0} Success!", url));
}
这时候我们就可以按照参数来确定是否使用缓存。

4.我们可以自定义缓存输出类,实现自己的OutputCache

public class OutputCache:System.Web.Mvc.ActionFilterAttribute 
{
        public int Duration { get; set; }
        public CachePolicy CachePolicy { get; set; }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (CachePolicy == CachePolicy.Client || CachePolicy == CachePolicy.ClientAndServer)
            {
                if (Duration <= 0) return;

                //用于设置特定于缓存的 HTTP 标头以及用于控制 ASP.NET 页输出缓存
                HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
                TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetExpires(DateTime.Now.Add(cacheDuration));
                cache.SetMaxAge(cacheDuration);
                cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            }
     }
}
总结:

缓存在高性能web应用开发过程中作用非常重大,实现缓存的方式有很多,合理使用缓存能够非常有效提升用户体验。









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值