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

方法:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PaiXie.Pos.Admin {
/// <summary>
/// 缓存页面
/// </summary>
public class CacheFilterAttribute : ActionFilterAttribute {
/// <summary>
/// Gets or sets the cache duration in seconds. The default is 10 seconds.
/// </summary>
/// <value>The cache duration in seconds.</value>
public int Duration {
get;
set;
}
public CacheFilterAttribute() {
Duration = ;
}
public override void OnActionExecuted(ActionExecutedContext filterContext) {
if (Duration <= ) return;
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");
}
}
}

调用:

      [CacheFilter]
public ActionResult Index() {
}
上一篇:限制 button 在 3 秒内不可重复点击


下一篇:忘记Mysql的root密码怎么办?