会话和存储对象(Sessions and storing objects)

编程入门 行业动态 更新时间:2024-10-28 13:18:11
会话和存储对象(Sessions and storing objects)

如果将对象从会话(HttpContext.Current.Session)返回到对象类型的私有成员然后修改它,例如,您重命名属性或向对象内部列表成员添加内容。 如果在另一个页面上您从会话中请求相同的对象,那么这些更改是否会保留...是否会使用上一页上更新的值进行更新。 换句话说,对象是持久化的,或者在每个页面上更改任何内容后,您是否必须将对象重新保存回会话。 任何帮助深表感谢。 谢谢

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

最满意答案

如果是实际对象,则是,您正在修改由会话维护的版本,并且将保存其值。

如果它不是引用类型,如String,那么您必须引用实际的会话值来修改它(即:session [“key”] = value)。

但是,我会提醒您不要在会话中存储真实对象。 会话通过序列化和反序列化每个页面加载的值来工作。 您投入会话的次数越多,框架在开始执行您的页面之前必须执行的工作量越多。

由于它的工作原理,您实际上只应将值放在整个站点中真正需要的会话中。

在考虑将某些东西放入会议之前,我问自己以下几点:

这是整个网站还是靠近它需要的东西? 是否需要根据需要直接从数据库加载此信息,而不是从Web服务器内存中提取信息? 这个站点是负载平衡还是将来有可能实现负载均衡?

如果1为是,则使用会话是“可能” 如果2是肯定的,那么session也可能是,但我可能会使用不同的缓存机制。 如果3是肯定的话我根本不使用会话(并完全关闭它)。 负载平衡需要会话状态服务器,通常是sql server。 这导致我们回到会话的初始问题:即,为每个单页请求加载和保存值。

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.

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:

Is this something needed for the entire site or close to it? Is the time to load this information straight from the database on an as needed basis more than pulling it from the web servers memory? Is this site load balanced or has the potential of being load balanced in the future?

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.

更多推荐

本文发布于:2023-08-01 04:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Sessions   objects   storing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!