Asp.Net mvc 会话与缓存

发布于 2024-10-26 16:43:52 字数 774 浏览 0 评论 0原文

我们一直在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-11-02 16:43:52

在会话中为每个用户存储 30 个整数就可以了。

但现实是,使用缓存可能会遇到需要注意的问题:

  • 会话状态(除非使用状态服务器或 SQL 服务器)将是易失性,并且可能会因回收而丢失。此外,如果您将站点移动到多个服务器,并且使用易失性会话,则必须在所有服务器上填充会话。
  • 许多 ASP NET 功能(例如身份验证”是基于 cookie 的,而不是基于会话的)。您需要注意它们不能协同工作。例如,如果您使用表单身份验证并重新启动站点或回收应用程序,则用户将保持身份验证状态,而您会丢失所有会话信息。

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.
水晶透心 2024-11-02 16:43:52

会话似乎是一个选择,但我
已附加网络表格用于显示
水晶报表和会话对象
Webforms 和 MVC 使用的不同:
Mvc 使用 HttpSessionStateBase 和
网络表单使用 HttpSessionState。

HttpSessionStateBase 是一个抽象类,它的实现只是原始 HttpSessionState 对象的包装。因此,只要您停留在同一个应用程序中,MVC 和 WebForms 之间的会话就完全相同,您就可以使用它共享数据。因此,在您的场景中,会话似乎是持久数据的正确方法。

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.

迎风吟唱 2024-11-02 16:43:52

为此使用会话没有什么问题。会没事的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文