I have an MVC .net site and I am attempting to leverage OutputCache for performance gain.
/// <summary>
/// Broker View Page
/// </summary>
/// <returns></returns>
[Route("{lang}/brokers/details/{id}/{code}", Order = 1)]
[Route("brokers/details/{id}/{code}", Order = 2)]
[OutputCache(Duration = (60 * 60), VaryByParam = "none")]
public ActionResult View(int? id, string code)
{
This delivers a huge performance gain on 2nd and subsequent visits to a site, but I have just discovered one huge Gotcha!
If a client visits the page anonymously, subsequently logs in and returns to the page, they are still served up the unauthenticated view (authenticated clients should see the same content, but different header)
is there any way I can use OutputCache to keep my performance gain, but have it smart enough to know about authenticated/unauthenticated differences?
You can use "VaryByCustom"
In controller
[OutputCache(Duration = 1000, VaryByCustom = "user")]
public ActionResult Index()
{
return View();
}
In Global.ascx.cs :
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "user")
{
if (context.Request.IsAuthenticated)
{
return context.User.Identity.Name;
}
}
return "anonymous"
}
Each user gets their own unique cached version version and there is a cached version created for "anonymous" users too.