为什么应该或不应该将数据集、数据表等作为会话变量存储在 ASP.NET 页面中?

Why should or shouldn#39;t I store a Dataset, Datatable, etc as a session variable in an ASP.NET page?(为什么应该或不应该将数据集、数据表等作为会话变量存储在 ASP.NET 页面中?)

本文介绍了为什么应该或不应该将数据集、数据表等作为会话变量存储在 ASP.NET 页面中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Web 服务返回的数据集的 Web 应用程序.

I am working on a web application that is consuming a Dataset returned by a web service.

当应用程序运行时,我将该数据集存储为会话变量,以便在用户导航到将编辑数据集中表的不同页面时反复使用.

As the application is running I store that Dataset as a session variable to be used over and over again as the user navigates to the different pages that will edit the tables within the dataset.

这个想法是用户只需要在应用程序加载时等待数据一次,然后应用程序将使用会话变量,直到用户保存他们所做的更改,当这种情况发生时,它会将编辑过的表传递给服务更新数据库.

The idea was the user will only have to wait for the data once when the application loads then the application would use the session variable until the user saves the changes they made, when that happens it would pass the edited tables to the service to update the database.

这种设计以及将数据集和数据表存储为会话变量是否存在问题?利弊告诉我.

Is there problems with this design and the storage of the Dataset and Datatables as a session variable? Pros and Cons let me know.

推荐答案

唯一的优点是:

  • 这比重复访问数据库要快.也就是说,企业数据库可以提供足够的缓存,而您通常不需要这样做.

缺点很多,但主要的三个是:

The cons are legion but the main three would be:

  • 如果数据从另一个 Session(比如管理员用户)更改,您的用户的 Session 不知道
  • 内存分配很快就会成为一个严重的问题(尽管这可以通过使用缓存而不是会话并键入会话 ID 来缓解)
  • 如果您曾经搬到服务器场,您将不得不重新考虑您的整个设计,很可能使用数据库来存储会话数据 - 那么您的效率论点就会显得有点弱

正如其他人指出的那样,将任何内容保存在 Session 或 Cache(或在较小程度上的应用程序)中也存在问题,而无需将其保存在更永久的地方.如果 Session 过期,甚至重置自己(我已经看到导致 Session 开始和结束每个请求的硬件配置.虽然它保留了 Session ID,但存储在 Session 对象中的任何数据都会丢失.如果应用程序重新启动,无论原因是,所有的 Session、Cache 和 Application 对象都被清除了.缓存对象可以随时清除自己,只是因为环境决定它需要该内存空间用于其他用途.

As others have pointed out, there is also a problem with saving anything in Session or Cache (or to a lesser extent Application) without persisting it somewhere more permanent as well. If a Session expires, or even resets itself (I have seen hardware configurations that cause the Session to begin and end with every request. Though it retains the Session ID, any data stored in Session objects is lost. If the Application is restarted for whatever reason, all of the Session, Cache and Application objects are cleared down. Cache objects can clear theselves at any time, just because the environment decides it wants that memory space for something else.

总结:这不是要走的路.

In summary: This is not the way to go.

如果您需要用户能够进行更改,然后点击保存"以将其持久化,请保留一组单独的状态表,其中详细说明在用户点击保存"时应对主表进行的更改.在客户端上存储一个 cookie,其密钥在很长一段时间内都会过期;使用该 cookie 将您的状态数据与用户匹配.

If you need the user to be able to make changes and then hit Save to persist them, keep a separate set of State tables that detail the changes that should be made to the main tables when the user hits Save. Store a cookie on the client with a key which expires a long way into the future; use that cookie to match your State data to the User.

这篇关于为什么应该或不应该将数据集、数据表等作为会话变量存储在 ASP.NET 页面中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么应该或不应该将数据集、数据表等作为会话变量存储在 ASP.NET 页面中?

基础教程推荐