Asp.Net mvc 会话与缓存
我们一直在开发 ASP.NET MVC 应用程序。只要用户登录,我们就必须维护一些特定于用户的信息。我有什么选择?缓存在站点的所有用户之间共享。会话似乎是一个选项,但我附加了用于显示 Crystal Reports 的 Web 表单,并且 Web 表单和 MVC 使用的 Session 对象是不同的:Mvc 使用 HttpSessionStateBase,而 Web 表单使用 HttpSessionState。
我应该在会话中保留多少信息(我有一个大约 30 个整数/用户的数组)?我应该将其保留在会话中还是应该使用会话和缓存的某种组合?
编辑:我创建了一个 SessionService,它接受 HttpSessionStateBase 作为参数,并且从 Mvc 中调用它:
SessionService _service = new SessionService(HttpContext.Session);
它返回 HttpSessionStateBase,但正如 Darin 建议的那样,它是抽象类,因此它也可能使用 SessionStateWrapper 实现。在网络表单场景中,我使用类似的东西
HttpSessionStateWrapper Session = new HttpSessionStateWrapper(HttpContext.Current.Session);
SessionService _SessionRepository = new SessionService(Session);
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 webforms 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在会话中为每个用户存储 30 个整数就可以了。
但现实是,使用缓存可能会遇到需要注意的问题:
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:
HttpSessionStateBase
是一个抽象类,它的实现只是原始HttpSessionState
对象的包装。因此,只要您停留在同一个应用程序中,MVC 和 WebForms 之间的会话就完全相同,您就可以使用它共享数据。因此,在您的场景中,会话似乎是持久数据的正确方法。HttpSessionStateBase
is an abstract class and it's implementation is simply a wrapper of the originalHttpSessionState
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.