當(dāng)數(shù)據(jù)上到好幾萬多條時(shí),要調(diào)用幾條數(shù)據(jù)在頁面顯現(xiàn),ASP就會(huì)慢如蝸牛.最糟糕的是,當(dāng)n多用戶打開頁面拜訪的時(shí)分,每個(gè)用戶每次都要讀取數(shù)據(jù)庫一次,
這無疑降低了效率,很明顯, 假如能把數(shù)據(jù)能保具有內(nèi)存上,然后讀取,無疑放慢了速度.
所謂緩存其實(shí)就是在內(nèi)存中開辟一個(gè)用來保存數(shù)據(jù)的空間.
使用緩存你就不必屢次的拜訪你保具有硬盤上的數(shù)據(jù)了,由于這些數(shù)據(jù)我們希望每個(gè)用戶都能看到成效一樣,考慮使用的是application對象,由于它是
一切拜訪者的共用的對象,存儲(chǔ)的消息和定義的事情能夠?yàn)橐磺姓甙菰L者使用,這里要使用asp內(nèi)置對象application了.
關(guān)于application:
2個(gè)辦法[lock 和 unlock],
2個(gè)集合[content 和 staticobjects],
2個(gè)事情[開端的application_onstart 和 application_end]
application變量不會(huì)由于用戶的分開而消失,一旦建立,不斷等到網(wǎng)站關(guān)閉和程序卸載為止,正由于如此,使用的時(shí)分要特別當(dāng)心!,否則會(huì)占用內(nèi)存
(虛擬主機(jī)提供商很不高興了).
我們是把數(shù)據(jù)寫入一個(gè)自定義的application里面,在制定的工夫讀取刷新的,大致思緒就是這樣.
實(shí)例演示.先建立一個(gè)簡單的數(shù)據(jù)庫,寫個(gè)function讀取一下,寫入一個(gè)dim變量temp中:
程序代碼
<% Function DisplayRecords()
'這個(gè)函數(shù)本來給一個(gè)變量temp付上記載的值
Dim sql, conn, rs
'符合條件的sql語句
sql = "Select id, [szd_f], [szd_t] FROM admin"
'打開數(shù)據(jù)庫連接
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};
DBQ="&Server.MapPath("db.mdb")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 3
'當(dāng)符合sq語句l的數(shù)據(jù)沒有顯現(xiàn)完畢時(shí)
If Not rs.EOF Then
'給temp變量賦值
Dim temp
temp = "<table width=""90%"" align=""center"""
temp = temp & " border=""1"" bordercolor=""silver"""
temp = temp & " cellspacing=""2"" cellpadding=""0"">"
temp = temp & "<tr bgcolor=""#CCDDEE""><td width=""5%"""
temp = temp & ">ID</td><td>操作</td>"
temp = temp & "<td>數(shù)值</td></tr>"
While Not rs.EOF
temp = temp & "<tr><td bgcolor=""#CCDDEE"">"
temp = temp & rs("ID") & "</td><td>" &
rs("szd_f")
temp = temp & "</td><td>" & rs("szd_t")
temp = temp & "</td></tr>"
rs.MoveNext
Wend
temp = temp & "</table>"
'temp賦值完畢,把它再前往給函數(shù)
DisplayRecords = temp
Else
DisplayRecords = "Data Not Available."
End If
'開釋內(nèi)存
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
End Function %>
ok,上面的函數(shù)改造完畢,調(diào)用的時(shí)分就是DisplayRecords.
上面是application大顯本領(lǐng)了:
程序代碼
<%'該函數(shù)是寫入緩存
Function DisplayCachedRecords(Secs)
Dim retVal, datVal, temp1
'Secs是每次要刷新數(shù)據(jù)的工夫, retVal是數(shù)據(jù),datVal是剩余工夫
retVal = Application("cache_demo") '獲得appliction的值
datVal = Application("cache_demo_date") '獲得appliction的值
'判別datVal 的值,也就是要計(jì)算工夫過來了沒
If datVal = "" Then
'假如是空,datVal值為當(dāng)前工夫按秒加上secs定義的工夫
datVal = DateAdd("s",Secs,Now)
End If
'temp1是判別當(dāng)前工夫和datVal的秒差
temp1 = DateDiff("s", Now, datVal)
'假如retVal已經(jīng)是上面函數(shù)的前往值且工夫大于0
If temp1 > 0 And retVal <> "" Then
'本函數(shù)前往記載數(shù)
DisplayCachedRecords = retVal
Response.Write "<b><font color=""green"">利用緩存讀取數(shù)據(jù)"
Response.Write " ... (" & temp1 & "
秒剩余)</font></b>"
Response.Write "<br><br>"
Else
'retVal 是空的話,就賦予DisplayRecords的值給變量temp2
Dim temp2
temp2 = DisplayRecords()
'保存到Application.------------------>重點(diǎn)
Application.Lock
Application("cache_demo") = temp2
Application("cache_demo_date") = DateAdd("s",Secs,Now)
Application.UnLock
DisplayCachedRecords = temp2
' 這里隨便寫上了記載的緩存的過來工夫,絕對總秒數(shù)倒差 :
Response.Write "<b><font color=""red"">刷新緩存顯現(xiàn) ..."
Response.Write "</font></b><br><br>"
End If
End Function
%>
說明完畢.
調(diào)用辦法:<%=DisplayCachedRecords(20)%>