|
|
|
7i24.Com不停为您服务 缓存数据
甚麽是缓存(cache)? 缓存是将相对高速的存储设备作为相对低速的存储设备与系统之间I/O的缓冲区,它能大幅提高系统的性能.
对于Web站点来说,缓存数据就是将用户每次访问你的站点时需要动态生成的信息预先生成并存储在内存中,以静态的形式送给用户. 这么说太抽象了,让我们来举个例子说明甚麽是站点的缓存. 假设你的站点有一个更新不是很频繁的下拉式列表框,列表框中的选项当用户访问站点时从数据库中取出,我们可以将这些选项预先取出,存放在内存中以提高性能.
Application Object
Application Object能保存整个应用的全局信息. 换言之,它存储着站点的全局信息. 每个站点都被认为是一个应用,你可以在global.asa中利用Application Object存储你的信息.
让我们来看看示例程序. 在这个例子里,我将Application Object作为一个变量用来存储下拉菜单的菜单项. 当每次需要从数据库中读取菜单项的时候,我们从变量中读取,避免了对数据库的频繁访问.
请看以下的代码(我写了详尽的注释来解释每行代码的含意):
<%= Application("ListBox")%>
<%
'here we define the variable'
as an a application object
ListBox = Application("ListBox")
'we then check to see if it is already set
If ListBox = "" Then
'it's not, so we go to the database and retrieve it
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "DSN=travel;UID=;PWD="
sql = "SELECT * FROM types"
Set rs = oConn.Execute(sql)
crlf = chr(13) & chr(10)
'Now we assign the recordset to the 'ListBox'
'variable using a loop
ListBox = "<select name='listbox'>" & crlf
Do Until rs.EOF
Listbox = ListBox & " <option>" & _
rs("type") & "</option>" & crlf
rs.MoveNext
Loop
'we then assign the variable to the
'Application object below
Application("ListBox") = ListBox
End If
%>
从代码中我们可以看出,数据只有第一次被读取时访问数据库,然后将之存储在
Application object中,以后每次都从缓存中读取,这样将会加快站点的速度. 并不是每个站点都需要缓存来提高速度,但如果你的站点上运行着复杂的应用程序,且你想让系统获得最优的性能,那么你最好考虑一下缓存数据
|
|
|