mvc 缓存页面 减轻服务器压力

方法:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 namespace PaiXie.Pos.Admin {
 7     /// <summary>
 8     /// 缓存页面
 9     /// </summary>
10     public class CacheFilterAttribute : ActionFilterAttribute {
11         /// <summary>
12         /// Gets or sets the cache duration in seconds. The default is 10 seconds.
13         /// </summary>
14         /// <value>The cache duration in seconds.</value>
15         public int Duration {
16             get;
17             set;
18         }
19         public CacheFilterAttribute() {
20             Duration = 60;
21         }
22         public override void OnActionExecuted(ActionExecutedContext filterContext) {        
23             if (Duration <= 0) return;
24             HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
25             TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);
26             cache.SetCacheability(HttpCacheability.Public);
27             cache.SetExpires(DateTime.Now.Add(cacheDuration));
28             cache.SetMaxAge(cacheDuration);
29             cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
30         }    
31     }
32 }
View Code

调用:

1      [CacheFilter]
2         public ActionResult Index() {
3 }
View Code
原文地址:https://www.cnblogs.com/lyl6796910/p/5213334.html