开发者

what is the best way to cache user information in my basecontroller in asp.net mvc

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开发者_JAVA技巧 also not cache one users info when another user logs on?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜