0

I'm trying to make my homepage load as fast as possible so I want to utilise OutputCache. But I want to show a different homepage depending on whether the user is logged in (and in a specific role).

This is my current code in my HomeController. I know I can use VaryByParam and pass a parameter but that's not going to work if users are requesting the root of my domain. Do I just have to settle for using MemoryCache to cache the model data instead?

I've tried varying the cache by cookies as described here OutputCache VaryByCustom cookie value but I can't get it to work.

I think all I need is to be able to detect if a user is logged in before OutputCache is returned and then I can redirect them to another page if they are?

[OutputCache(Duration = 604800, Location = System.Web.UI.OutputCacheLocation.Server)]//1 week
public ActionResult Index()
{
    if (HttpContext != null && !String.IsNullOrEmpty(HttpContext.User.Identity.Name))
    {
        var user = userService.GetUserByEmail(HttpContext.User.Identity.Name);
        if (user != null && user.IsUserSubscribed)
        {
             //get model data and show a different view if user is subscribed
        }
        else
        {
            //get model data and show different view if user logged in but not subscribed
        }
    }
    else
    {
        //get model data and show default view
    }
}
Jimmy
  • 2,191
  • 6
  • 25
  • 45

0 Answers0