Mvc 页面缓存 OutputCache VaryByCustom
生活随笔
收集整理的這篇文章主要介紹了
Mvc 页面缓存 OutputCache VaryByCustom
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
優化網站,dotNet MVC 可以通過(OutputCache)特性在某些Action上使用緩存,如果我們想要自定義緩存依據可以通過如下方式進行:
第一步, 在 global.asax.cs 文件中 overite GetVaryByCustomString函數:
/// <summary>/// 自定義生成的依據/// </summary>/// <param name="context"></param>/// <param name="custom"></param>/// <returns></returns>public override string GetVaryByCustomString(HttpContext context, string custom) {if (EqualsIgnoreCase("AnonymousID", custom)) {return context.Request.AnonymousID;}return base.GetVaryByCustomString(context, custom);}// System.Web.Util.StringUtilstatic bool EqualsIgnoreCase(string s1, string s2) {return (string.IsNullOrEmpty(s1) && string.IsNullOrEmpty(s2)) ||(!string.IsNullOrEmpty(s1) && !string.IsNullOrEmpty(s2) && s2.Length == s1.Length &&string.Compare(s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase) == 0);}第二步:在需要輸出緩存的Action上添加OutPutCache特性:
[OutputCache(CacheProfile = "StaticPage")]public ActionResult Index(){return View();}第三部:為了便于修改緩存配置,我們可以辦具體的緩存設置寫在配置文件中,如下:
<caching><outputcachesettings> <outputcacheprofiles><clear /><!-- 24 hours--><add varybycustom="AnonymousID" varybyparam="*" duration="86400" name="StaticPage" /></outputcacheprofiles></outputcachesettings></caching>轉載于:https://www.cnblogs.com/lcxmvc/p/4628842.html
總結
以上是生活随笔為你收集整理的Mvc 页面缓存 OutputCache VaryByCustom的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 制作win7+ubuntu +winPE
- 下一篇: 【effective c++读书笔记】【