Mvc 页面缓存 OutputCache VaryByCustom
生活随笔
收集整理的這篇文章主要介紹了
Mvc 页面缓存 OutputCache VaryByCustom
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
優(yōu)化網(wǎng)站,dotNet MVC 可以通過(guò)(OutputCache)特性在某些Action上使用緩存,如果我們想要自定義緩存依據(jù)可以通過(guò)如下方式進(jìn)行:
第一步, 在 global.asax.cs 文件中 overite GetVaryByCustomString函數(shù):
/// <summary>/// 自定義生成的依據(jù)/// </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();}第三部:為了便于修改緩存配置,我們可以辦具體的緩存設(shè)置寫在配置文件中,如下:
<caching><outputcachesettings> <outputcacheprofiles><clear /><!-- 24 hours--><add varybycustom="AnonymousID" varybyparam="*" duration="86400" name="StaticPage" /></outputcacheprofiles></outputcachesettings></caching>轉(zhuǎn)載于:https://www.cnblogs.com/lcxmvc/p/4628842.html
總結(jié)
以上是生活随笔為你收集整理的Mvc 页面缓存 OutputCache VaryByCustom的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 制作win7+ubuntu +winPE
- 下一篇: 在Ext JS 6中添加本地化包