[2 [2
ASP.NET MVC的缓存机制可显着提高性能。 但是,存在禁用特定操作缓存的方案对于确保检索新鲜数据至关重要。本指南详细详细介绍了使用自定义属性在特定ASP.NET MVC操作中缓存的方法。
[2
为了构建一个禁用缓存的自定义属性,我们利用
[2 公共密封级nocacheattribute:ActionFilterAttribute { 公共覆盖void onResultExeCuting(ResultexecutingContextContextContext) { FilterContext.httpContext.Response.cache.setExpires(dateTime.utcnow.addays(-1)); FilterContext.httpContext.Response.Cache.SetValiduntilexpires(false); FilterContext.httpContext.Response.Cache.SetRiDation(httpcacherevalidation.allcaches); FilterContext.httpContext.Response.Cache.setCacheability(httpcacheability.nocache); FilterContext.httpContext.Response.cache.setNostore(); base.onresultexecuting(filterContext); } }
将应用于控制器或操作方法禁用该特定元素的缓存。 另外,从基本控制器继承并使用
[nocache] 对其进行装饰可防止所有继承控制器的缓存。
在数据检索中使用jQuery时,在[
[2
缓存:false,
// ...其他AJAX设置
});
[2
在实施反机械措施后,“硬刷新”(CTRL F5)对于确保浏览器不依赖缓存数据至关重要。如果浏览器保留了缓存版本,则标准刷新(F5)可能并不总是检索最新信息。
概括
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
cache:false 有效,有效地确保了浏览器MVC操作的有效速度有效,很有效地有效。 掌握缓存控件是避免过时数据影响用户体验和应用程序逻辑的关键。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3