You could use the VaryByCustom. In Global.asax override the GetVaryByCustomString
method:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "IsLoggedIn")
{
if (context.Request.Cookies["anon"] != null)
{
if (context.Request.Cookies["anon"].Value == "false")
{
return "auth";
}
else
{
return "anon";
}
}
else
{
return "anon";
}
}
else
{
return base.GetVaryByCustomString(context, arg);
}
}
and then use the OutputCache attribute:
[OutputCache(CacheProfile = "MyProfile")]
public ActionResult Index()
{
return View();
}
and in web.config:
<caching>
<outputcachesettings>
<outputcacheprofiles>
<clear />
<add varybycustom="IsLoggedIn" varybyparam="*" duration="86400" name="MyProfile" />
</outputcacheprofiles>
</outputcachesettings>
</caching>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…