所謂緩存其實(shí)就是在內(nèi)存中開(kāi)辟一個(gè)用來(lái)保存數(shù)據(jù)的空間,使用緩存你就不用頻繁的訪問(wèn)你保存在硬盤(pán)上的數(shù)據(jù)了,因?yàn)檫@些數(shù)據(jù)我們希望每個(gè)用戶都能看到效果一 樣,考慮使用的是application對(duì)象,因?yàn)樗撬性L問(wèn)者的共用的對(duì)象,存儲(chǔ)的信息和定義的事件能夠?yàn)樗姓咴L問(wèn)者使用,這里要使用asp內(nèi)置對(duì) 象APPLICATION了,關(guān)于application,有2個(gè)方法[lock和unlock],2個(gè)集合[content和 staticobjects],2個(gè)事件[開(kāi)始的application_onstart和application_end],application變 量不會(huì)因?yàn)橛脩舻碾x開(kāi)而消失,一旦建立,一直等到網(wǎng)站關(guān)閉和程序卸載為止,正因?yàn)槿绱?使用的時(shí)候要特別小心!,否則會(huì)占用內(nèi)存,我在這里不用多說(shuō),有興 趣的查閱相關(guān)資料吧,大體是這樣.我們是把數(shù)據(jù)寫(xiě)入一個(gè)自定義的application里面,在制定的時(shí)間讀取刷新的,大體思路就是這樣.
實(shí)例演示.先建立一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù),寫(xiě)個(gè)function讀取一下,寫(xiě)入一個(gè)dim變量temp中:
以下是引用片段:
復(fù)制代碼 代碼如下:
Function DisplayRecords()
'這個(gè)函數(shù)原來(lái)給一個(gè)變量temp付上記錄的值
Dim sql, conn, rs
'符合條件的sql語(yǔ)句
sql = "SELECT id, [szd_f], [szd_t] FROM admin"
'打開(kāi)數(shù)據(jù)庫(kù)連接
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語(yǔ)句l的數(shù)據(jù)沒(méi)有顯示完畢時(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大顯身手了:
復(fù)制代碼 代碼如下:
'該函數(shù)是寫(xiě)入緩存
Function DisplayCachedRecords(Secs)
Dim retVal, datVal, temp1
'Secs是每次要刷新數(shù)據(jù)的時(shí)間, retVal是數(shù)據(jù),datVal是剩余時(shí)間
retVal = Application("cache_demo") '取得appliction的值
datVal = Application("cache_demo_date") '取得appliction的值
'判斷datVal 的值,也就是要計(jì)算時(shí)間過(guò)去了沒(méi)
If datVal = "" Then
'如果是空,datVal值為當(dāng)前時(shí)間按秒加上secs定義的時(shí)間
datVal = DateAdd("s",Secs,Now)
End If
'temp1是判斷當(dāng)前時(shí)間和datVal的秒差
temp1 = DateDiff("s", Now, datVal)
'如果retVal已經(jīng)是上面函數(shù)的返回值且時(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
' 這里隨便寫(xiě)上了記錄的緩存的過(guò)去時(shí)間,相對(duì)總秒數(shù)倒差 :
Response.Write "b>font color=""red"">刷新緩存顯示 ..."
Response.Write "/font>/b>br>br>"
End If
End Function
%>
說(shuō)明完畢.
以下為完整無(wú)注釋代碼
調(diào)用方法:%=DisplayCachedRecords(20)%>
寫(xiě)在后面的話:如果你感覺(jué)你的服務(wù)器內(nèi)存不夠大的話,不要大量使用緩存.