如何對(duì)蜘蛛機(jī)器人添加緩存,OutputCache應(yīng)該如何寫緩存?
廣告:
如何對(duì)蜘蛛機(jī)器人添加緩存,OutputCache應(yīng)該如何寫緩存?
ASP.NET MVC Prevent OutputCache if request is from a spider
[OutputCache(Duration = 7200, VaryByParam = "none")]
如何對(duì)蜘蛛機(jī)器人添加緩存,應(yīng)該如何寫緩存?
網(wǎng)站首頁(yè)被設(shè)置對(duì)任何請(qǐng)求是一樣的緩存。這里有個(gè)問(wèn)題,如果是正常用戶訪問(wèn),網(wǎng)站則是正常的,如果第一次請(qǐng)求是從蜘蛛發(fā)起的,則情況不一樣了,網(wǎng)站首頁(yè)變成下載的頁(yè)面了 。
step 1. Load page with normal user agent. (Output cache caches the URL)
step 2. Load page with spider user agent. (the previous cached response is sent to the spider, and my Phantom JS filter never runs)
第一步:正常用戶客戶端,輸出緩存正常
第二步:蜘蛛機(jī)器人客戶端,緩存輸出是針對(duì)蜘蛛客戶端的。則首頁(yè)正常用戶訪問(wèn)變成了下載。
解決方法:
Use VaryByCustom to force a 'Cache Miss' when the request is from a Search Engine Crawler(搜索引擎爬蟲).
In your Controller/Action:
[OutputCache(VaryByCustom="Crawler")]
public ActionResult Index()
{
// ...
return View();
}
Then in your Global.asax:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "Crawler" && context.Request.Browser.Crawler)
return Guid.NewGuid().ToString();
return base.GetVaryByCustomString(context, arg);
}
廣告: