views:

32

answers:

2

on each controller call i have a code that gets some user properties from a webservice. I want to cache these for each user so i only hit the webservice once.

what is the best way to cache code in a controller method so it will allow me to avoid hitting the database on every URL request but also not cache one users info when another user logs on?

+1  A: 

Check out the caching piece of the Enterprise library. We use it at work to cache lookups, so you only hit our WCF services once, instead of thousands of times for the exact same data.

You can also use Session, which I highly advise against unless your user is very small.

if (Session("user") == null)
{
    Session("user") = CallWebService.GetUser(userId);
}

Why you should keep Session small, for this webpage:

Avoid storing too much data in session variables, and make sure your session timeout is reasonable. This can use a significant amount of server memory. Keep in mind that data stored in session variables can hang out long after the user closes the browser. Too many session variables can bring the server on its knees.

Martin
@Martin - this seems like a bit of overkill. is there not a simpler solution (an attribute or something) to say cache per user ??)
ooo
@ooo - I edited it for another option, using Session
Martin
@Martin - what is the reason you advise against using session ?? What do you mean unless your "user" is very small ??
ooo
+2  A: 

You could use the ASP.NET session to store per user values.

Darin Dimitrov