前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP NET MVC OutputCache

ASP NET MVC OutputCache

作者头像
全栈程序员站长
发布2022-09-15 10:08:16
1.1K0
发布2022-09-15 10:08:16
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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

1.OutputCache 简单Demo

代码语言:javascript
复制
[OutputCache(Duration = 60)]
public ActionResult Index()
{
      ViewBag.date = DateTime.Now.ToString();
      return View();
}

输出页面

代码语言:javascript
复制
<br />
@ViewBag.date

效果:

代码语言:javascript
复制
2015/6/10 10:40:08

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

删除缓存:

代码语言:javascript
复制
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.带参数的缓存

代码语言:javascript
复制
[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

输出:

代码语言:javascript
复制
Index2
2015/6/10 10:45:19 
1

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

代码语言:javascript
复制
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.多个参数的缓存

代码语言:javascript
复制
[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

代码语言:javascript
复制
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应用开发过程中作用非常重大,实现缓存的方式有很多,合理使用缓存能够非常有效提升用户体验。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162891.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档