Using cache during user session

0

I'm using OutputCache in an asp.net mvc5 application to cache a list in my application this way:

[(Duration = 60, VaryByParam = "none")]
public ActionResult Index()
{}

I just need to take advantage of this cache while the user is logged into the application. ie I will only load the list again in the new user login.

How can I define this?

    
asked by anonymous 09.05.2018 / 16:32

1 answer

0

For this you can write your own rule to the cache using VaryByCustom .

In this case, you need to write the GetVaryByCustomString method and it will return whether or not to use the cache based on Session . Assuming you have, for example, a session with the user login called "Login", you could do so:

[(Duration = 60, VaryByParam = "none" aryByCustom="SessionLogin")]
public ActionResult Index() {  }

And write this method in global.asax :

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
  if(arg == "SessionLogin") 
  { 
    var login = context.Request.Session["Login"]; 
    if (login != null) 
      return login.ToString(); 
  } 
  return base.GetVaryByCustomString(context, arg); 
}
    
09.05.2018 / 16:40