如何对蜘蛛机器人添加缓存,OutputCache应该如何写缓存?
广告:
如何对蜘蛛机器人添加缓存,OutputCache应该如何写缓存?
ASP.NET MVC Prevent OutputCache if request is from a spider
[OutputCache(Duration = 7200, VaryByParam = "none")]
如何对蜘蛛机器人添加缓存,应该如何写缓存?
网站首页被设置对任何请求是一样的缓存。这里有个问题,如果是正常用户访问,网站则是正常的,如果第一次请求是从蜘蛛发起的,则情况不一样了,网站首页变成下载的页面了 。
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)
第一步:正常用户客户端,输出缓存正常
第二步:蜘蛛机器人客户端,缓存输出是针对蜘蛛客户端的。则首页正常用户访问变成了下载。
解决方法:
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);
}
广告: