花雕卫浴是几线牌啊:<%session("OldGuestSave")=""%>是什么意思

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 01:44:10
最好能帮我介绍一下session,我对它不太了解,很模糊!

session("OldGuestSave")="" 将用户全局变量OldGuestSave的值改成空

session定义的是用户全局变量,每个用户连接到服务器时,都会拥有一个session,该session定义的变量只有该用户能读写,并且在任何页面都可以读写,当用户关闭浏览器时该session被清空.session变量会占用额外的内存空间,如非必要,尽量少用.

与其类似的还有一个application应用程序变量,同样也是全局变量,不过application在IIS服务启动时就被创建,直到IIS服务停止,并且application定义的变量是任何用户都可以读写.

用Application对象或Session对象缓存常用数据
ASP的Application和Session对象是一个极其方便的在内存中缓存数据的容器。你可以把数据放到Application或Session对象中,这些数据就会在整个HTTP调用中一直存在。每个用户有自己的Session对象中的数据,而Application对象中的数据可以在所有用户中共享。

应该在什么时候将数据装入Application或Session中呢?通常,数据在Application或Session启动的时候装入。要想在Application或Session启动的时候装入数据,需要分别在Global.asa的Application_OnStart()或Session_OnStart()中添加适当
的代码;如果Global.asa中没有这两个函数,你可以手工添加。也可以在数据第一次使用的时候将其装入。要想这样,应该在ASP页面中写一些代码(或是写一个可重用的脚本函数)来检查数据是否存在并且如果数据不存在则将其装入内存。下面是一个经典的性能调整技术--Lazy Evaluation:

<%
Function GetEmploymentStatusList
Dim d
d = Application("EmploymentStatusList")
If d = "" Then
' FetchEmploymentStatusList function (not shown)
' fetches data from DB, returns an Array
d = FetchEmploymentStatusList()
Application("EmploymentStatusList") = d
End If
GetEmploymentStatusList = d
End Function
%>

Similar functions could be written for each chunk of data needed.

In what format should the data be stored? Any variant type can be
stored, since all script variables are variants. For instance, you
can store strings, integers, or arrays. Often, you’ll be storing the
contents of an ADO recordset in one of these variable types. To get
data out of an ADO recordset, you can manually copy the data into
VBScript variables, one field at a time. It’s faster and easier to
use one of the ADO recordset persistence functions GetRows(),GetString
() or Save() (ADO 2.5). Full details are beyond the scope of this
article, but here’s a function that demonstrates using GetRows() to
return an array of recordset data:

' 获取记录集,返回数组
Function FetchEmploymentStatusList
Dim rs
Set rs = CreateObject("ADODB.Recordset")
rs.Open "select StatusName, StatusID from EmployeeStatus", _
"dsn=employees;uid=sa;pwd=;"
FetchEmploymentStatusList = rs.GetRows() ' 将记录集用数组返回
rs.Close
Set rs = Nothing
End Function

A further refinement of the above might be to cache the HTML for the
list, rather than the array. Here’s a simple sample:

' 获取记录集,返回HTML Option列表
Function FetchEmploymentStatusList
Dim rs, fldName, s
Set rs = CreateObject("ADODB.Recordset")
rs.Open "select StatusName, StatusID from EmployeeStatus", _
"dsn=employees;uid=sa;pwd=;"
s = "<select name=""EmploymentStatus">" & vbCrLf
Set fldName = rs.Fields("StatusName") ' ADO 字段绑定
Do Until rs.EOF
s = s & " <option>" & fldName & "</option>" & vbCrLf
rs.MoveNext
Loop
s = s & "</select>" & vbCrLf
rs.Close
Set rs = Nothing ' 释放rs
FetchEmploymentStatusList = s ' 用字符串方式返回数据
End Function

在正确情况下,你可以将ADO记录集本身缓存在Application或Session范围,但必须满足下面两个条件: .ADO必须被标记为自由线程模型(Free-threaded) .必须使用无连接记录集

如果不能满足上面两个条件,一定不要缓存记录集。在下面的“不灵活的组件”和“不要缓存Connection”两个技巧中,我们将讨论在Application和Session中保存COM对象的危险性。

当你在Application或Session中存储数据后,数据将一直保存,知道你的程序改变它,或是Session过期,或是Web服务重新启动。What if the data needs to be updated?手工刷新Application数据,可以调用只有管理员才可访问的用来刷新数据的ASP页面;或者定期的通过一个函数来周期性的更新数据。下面的例子在缓存数据中保存了一个时间戳,然后一段时间之后自动刷新数据。

<%
Const UPDATE_INTERVAL = 300 ' 刷新间隔,单位是秒

'返回雇员状态列表
Function GetEmploymentStatusList
UpdateEmploymentStatus
GetEmploymentStatusList = Application("EmploymentStatusList")
End Function

'周期性的更新缓存中的数据
Sub UpdateEmploymentStatusList
Dim d, strLastUpdate
strLastUpdate = Application("LastUpdate")
If (strLastUpdate = "") Or _
(UPDATE_INTERVAL < DateDiff("s", strLastUpdate, Now)) Then

' Note: two or more calls might get in here. This is okay and
will simply
' result in a few unnecessary fetches (there is a workaround
for this)

' FetchEmploymentStatusList function (not shown)
' fetches data from DB, returns an Array
d = FetchEmploymentStatusList()

' 更新Application对象时用Application.Lock()来保持数据一致性
Application.Lock
Application("EmploymentStatusList") = Events
Application("LastUpdate") = CStr(Now)
Application.Unlock
End If
End Sub

要知道在Session或Application中缓存大数组并不是一个太好的方法。在访问数组中的任何元素之前,脚本解释器都需要生成一个临时的整个数组的副本。例如,如果你缓存了一个100,000个字符串元素的数组,用来将邮政编码和当地的天气对应一一起来,在访问数组中任何一个字符串之前,ASP解释器首先必须复制所有的100,000个天气情况数据到一个临时数组中。在这种情况下,开发一个组件来储存天气情况数据或是使用词典(Dictioary)对象更为合适一点。不过,也不要因小失大,数组对象的的查找速度更快。索引一个词典比索引一个数组慢。你可以因你的情况而宜,选择合适的数据结构。