开发者

Asp.Net mvc session vs. cache

We have been developing asp.net mvc application. we have to maintain some user-specific information as long as user is logged in. What is the option for me? Cache is shared across all users of the site. Session seems to be the option but I have attached web开发者_运维问答forms for showing Crystal Reports and the Session object used by webforms and MVC is different: Mvc uses HttpSessionStateBase and webforms use HttpSessionState.

How much information should I keep in the session (I have an array of around 30 integers/user)? Should I keep it in the session or some combination of session and cache should be used instead?

Edit: I have created a SessionService that accepts HttpSessionStateBase as Parameter and from Mvc I'm calling it like:

SessionService _service = new SessionService(HttpContext.Session);

it returns HttpSessionStateBase but as Darin suggested it is abstract class so it might as well be using SessionStateWrapper implementation. In the web forms scenario I'm using something like

HttpSessionStateWrapper Session = new HttpSessionStateWrapper(HttpContext.Current.Session);
            SessionService _SessionRepository = new SessionService(Session);


Storing 30 integers per user in the session is fine.

But reality is, using cache you can get to problems that you need to be aware:

  • Session state - unless using state server or sql server - will be volatile and can be lost by recycling. Also if you move your site to multiple servers, you have to fill the session on all of them if you use volatile session.
  • Many ASP NET feature such as authentication are cookie based and not session based. You need to be aware that they do not work in tandem. For example, if you use forms authentication and restart the site or recycle the application, user stays authenticated while you have lost all your session information.


Session seems to be the option but i have attached webforms for showing crystal reports and the Session object used by webforms and MVC is different: Mvc uses HttpSessionStateBase and webforms use HttpSessionState.

HttpSessionStateBase is an abstract class and it's implementation is simply a wrapper of the original HttpSessionState object. So as long as you stay within the same application the session is exactly the same between MVC and WebForms and you would be able to share data using it. So in your scenario session seems a correct approach for persisting data.


Nothing wrong with using session for this. Will be fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜