会话和存储对象 [英] Sessions and storing objects

查看:92
本文介绍了会话和存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从会话(HttpContext.Current.Session)返回一个对象的对象类型的私有成员,然后修改它,例如重命名属性或添加一些物体内部列表成员。是这些变化如果持续另一页上,你从会话请求相同的对象... ...将其与更新previous页上的值进行更新。换句话说就是对象持久化,或者你要回来重新保存对象到会话一旦更改每个页面上的任何内容。任何帮助深表AP preciated。谢谢

If return an object from the session (HttpContext.Current.Session) to a private member of the objects type and then modify it, e.g. you rename an attribute or add something to the objects internal list member. Are those changes persisted if on another page you request the same object from the session...will it be updated with the values updated on the previous page. In other words is the object persisted, or do you have to re-save the object back to the session once you change anything on every page. Any help is much appreciated. Thanks

推荐答案

如果它是一个实际的对象,然后是的,你正在修改由会话维护版本,它的值将被保存。

If it is an actual object then yes, you are modifying the version that is maintained by session and it's values will be saved.

如果它不是一个引用类型,如String,那么你必须引用实际会话值进行修改。(如:会话[钥匙] =值)

If it is not a reference type, like String, then you have to reference the actual session value to modify it (ie: session["key"] = value).

不过,我要提醒你对会话存储实物。会议的工作原理是序列化和的每一个的页面加载反序列化值。你把会话甚至开始执行你的页面之前,更多的工作框架必须执行更多的。

However, I would caution you against storing real objects in session. Session works by serializing and deserializing the values on every single page load. The more you put into session the more work the framework has to perform before even beginning to execute your page.

由于它是如何运作的,你真的应该只把在会话值,你真正需要在整个网站中。

Because of how it works you really should only put values in session that you truly need across the entire site.

甚至考虑把东西会话之前我问自己以下内容:

Before even considering putting something in session I ask myself the following:


  1. 这是不是需要整个网站或接近它?

  2. 是从数据库中按照需求直接荷载信息多来自Web服务器的内存?
  3. 拉时间
  4. 这是网站的负载平衡或有被负载在未来平衡的潜力?

如果1是肯定的,然后使用会话是一个可能结果
如果2是肯定的,那么会话也是可能的,但我可能会使用不同的缓存机制。结果
如果3是肯定的话,我根本不使用会话(完全关闭)。负载均衡需要一个会话状态服务器,这通常是一个SQL服务器。这使我们回到最初的问题与会议:即值被加载并保存为每一个页面请求

If 1 is yes, then using session is a "maybe"
If 2 is yes, then session is also a maybe, but I'd probably use a different caching mechanism.
If 3 is a yes then I don't use session at all (and completely turn it off). Load balancing requires a session state server, which is usually a sql server. Which leads us back to the initial problem with session: namely, the values are loaded and saved for every single page request.

这篇关于会话和存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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