简体   繁体   English

ASP.NET SQLServer会话性能与局部变量

[英]ASP.NET SQLServer Session Performance vs. Local Variable

I am support a legacy application where I see code like this: 我支持一个遗留应用程序,在其中我看到如下代码:

'Use Session for everything?
If Session("UserType") = "Admin" Then
    'do admin something
ElseIf Session("UserType") = "Manager" Then
    'do manager stuff
ElseIf Session("UserType") = "User" Then
    'do regular stuff
Else
    'do anonymous stuff
End If

Would declaring a local variable, assigning the session variable to the local variable, then using the local variable for tests be faster and better for performance? 声明局部变量,将会话变量分配给局部变量,然后将局部变量用于测试会更快,更好地实现性能吗?

'Use local variable?
Dim UT As String = Session("UserType")
If UT = "Admin" Then
    'do admin something
ElseIf UT = "Manager" Then
    'do manager stuff
ElseIf UT = "User" Then
    'do regular stuff
Else
    'do anonymous stuff
End If

Or does IIS automatically cache the Session variable value once it retrieves it for the current executing code? 还是IIS为当前正在执行的代码检索到会话变量值后,它是否会自动缓存它?

From HttpApplication.PostAcquireRequestState Event we can see that an event is raised when the session state has been acquired: HttpApplication.PostAcquireRequestState事件中,我们可以看到在获取会话状态时引发了一个事件:

Occurs when the request state (for example, session state) that is associated with the current request has been obtained. 在获得与当前请求关联的请求状态(例如,会话状态)时发生。

ASP.NET Session State Overview states: ASP.NET会话状态概述指出:

Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext.Session property. 会话变量存储在通过HttpContext.Session属性公开的SessionStateItemCollection对象中。 In an ASP.NET page, the current session variables are exposed through the Session property of the Page object. 在ASP.NET页中,当前的会话变量通过Page对象的Session属性公开。

Putting those together, we can deduce that session values are not extracted from the request every time you use them, which appears to be your concern. 将它们放在一起,我们可以推断出每次使用会话值时都不会从请求中提取会话值,这似乎是您所关心的。 Calling it caching may be pushing the idea of caching a bit too far. 称它为高速缓存可能会使高速缓存的想法有点过分。

It is not IIS handling all that: it is ASP.NET. 不是IIS处理所有这些事情,而是ASP.NET。

Regarding the code you showed, personally I would prefer to see that handled with a Select Case , but it's up to you. 关于您显示的代码,就我个人而言,我希望看到它与Select Case一起处理,但这取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM