为什么应该或不应该在ASP.NET页面中存储数据集,Datatable等作为会话变量? [英] Why should or shouldn't I store a Dataset, Datatable, etc as a session variable in an ASP.NET page?

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

问题描述

我正在处理正在使用Web服务返回的数据集的Web应用程序。



随着应用程序的运行,我将该数据集存储为一个会话变量,以便一遍又一遍地使用,因为用户导航到不同的页面,这些页面将编辑数据集



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



此设计存在问题,并将数据集和数据库作为会话变量存储?优点和缺点让我知道。

解决方案

唯一的专业是:




  • 比反复访问数据库要快。也就是说,企业数据库做了足够的缓存,你不需要。



缺点是军团,但主要的三个:




  • 如果数据从另一个会话更改(例如管理员用户),则您的用户的会话不知道

  • 内存分配可能是一个非常快速的严重问题(尽管可以通过使用缓存而不是会话缓存来缓解会话ID)

  • 如果您曾经转到服务器农场,你将不得不重新思考你的整个设计,很可能使用数据库来存储会话数据 - 那么你的效率参数会看起来有点薄弱

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



总而言之:这不是要走的路。



如果您需要用户进行更改,然后点击保存来保存它们,请保留单独的一组状态表,详细说明当用户点击保存时应该对主表进行的更改。将cookie存储在客户端上,密钥将在以后过期很久;使用该cookie将您的状态数据与用户。


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 only pro is:

  • It's faster than repeatedly going to your database. That said, Enterprise databases do enough caching that you don't often need to.

The cons are legion but the main three would be:

  • If data is changed from another Session (say an admin user), your user's Session doesn't know
  • Memory allocation can be a serious issue very quickly (although this can be alleviated by using Cache instead of Session and keying on Session ID)
  • If you ever move to a server farm, you're going to have to rethink your entire design, quite possibly using the DB to store Session Data - then your efficiency argument is going to look a little weak

[Edit] 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.

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页面中存储数据集,Datatable等作为会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆