[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