存储在会话变量在整个页面生命周期反序列化一次或多次? [英] Is a variable stored in Session deserialized once or multiple times throughout a page lifecycle?

查看:102
本文介绍了存储在会话变量在整个页面生命周期反序列化一次或多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换以类似在$论述的方式Session变量C $的CProject

public static class WebSession
{
  private const string CurrentUserKey = "CurrentUser";

  private static HttpSessionState Session
  {
    get { return HttpContext.Current.Session; }
  }

  public static bool Exists
  {
    get { return Session != null; }
  }

  public static User CurrentUser
  {
    get { return Session[CurrentUserKey] as User; }
    set { Session[CurrentUserKey] = value; }
  }
}

下面是我的问题:如果我要访问的currentUser 在同一页面多次,我会通过将其分配到一个局部变量,而不是访问获得的性能提升包装的财产?还是在 HttpSessionState 确保对象只反序列化的每一次请求,这样,在同一个HTTP请求的后续调用不费话了?

Here is my question: if I have to access CurrentUser multiple times in the same page, would I get a performance improvement by assigning it to a local variable instead of accessing the wrapping property? Or does the HttpSessionState make sure the object is only deserialized once per request, so that subsequent calls in the same http request don't cost any more?

谢谢,
阿龙

推荐答案

还有就是你的会话状态对每个请求在内存中的副本。因此,你会通过在本地复制会话变量保存的唯一成本是从Object投你的类型的。然后在内存中副本添加到会话的请求结束。

There is an in-memory copy of your Session state on each request. Therefore the only cost that you would be saving by locally copying a session variable is that of the cast from Object to your type. The in-memory copy is then added to Session at the end of the request.

不管是不是会话序列化和反序列化页面上取决于你选择什么样的会话提供。对于在进程内会话状态,不会发生系列化。会话服务器的对象必须先序列化。

Whether or not Session is serialized and deserialized on a page depends on what Session Provider you choose. For In-proc Session State, no serialization occurs. For Session Servers the object must be serialized first.

这篇关于存储在会话变量在整个页面生命周期反序列化一次或多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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