Using cache during user session

Asked

Viewed 100 times

0

I am using Outputcache in an Asp.net mvc5 application to cache a list in my application as follows :

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

I just need to take advantage of this cache while the user is logged in to the application. ie I’ll just upload the list again in the new user login.

how can I define this?

1 answer

0

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

In this case, you need to write the method GetVaryByCustomString will either 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); 
}
  • But in the case would still have the duration of 60ms in the declaration, and that time does not have to be parameter while the user is logged in takes advantage it draws ? What is confusing for me, because I am obliged to declare this time.

  • The cache time will never beat the session time. Even if the session has an equal timeout, it can be renewed. If you use a higher parameter in Duration would work well, but still does not expire along with the session. If you want to be exactly the same as the session, it is easier to put the result of the Action in the session.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.