寫在前面的話:此篇還是asp相關(guān)的,相信玩ASP的都有這個(gè)感覺,當(dāng)數(shù)據(jù)有5萬多條時(shí)-------just like音樂網(wǎng),要調(diào)用最新的10條在頁面顯示,糟糕的是,當(dāng)n多用戶打開頁面訪問的時(shí)候,每個(gè)用戶每次都要讀取數(shù)據(jù)庫一次,這無疑降低了效率,很明顯,如果能把數(shù)據(jù)能保存在內(nèi)存上,然后讀取,無疑加快了速度. 所謂緩存其實(shí)就是在內(nèi)存中開辟一個(gè)用來保存數(shù)據(jù)的空間,使用緩存你就不用頻繁的訪問你保存在硬盤上的數(shù)據(jù)了,因?yàn)檫@些數(shù)據(jù)我們希望每個(gè)用戶都能看到效果一樣,考慮使用的是application對(duì)象,因?yàn)樗撬性L問者的共用的對(duì)象,存儲(chǔ)的信息和定義的事件能夠?yàn)樗姓咴L問者使用,這里要使用asp內(nèi)置對(duì)象APPLICATION了,關(guān)于application,有2個(gè)方法[lock和unlock],2個(gè)集合[content和staticobjects],2個(gè)事件[開始的application_onstart和application_end],application變量不會(huì)因?yàn)橛脩舻碾x開而消失,一旦建立,一直等到網(wǎng)站關(guān)閉和程序卸載為止,正因?yàn)槿绱?使用的時(shí)候要特別小心!,否則會(huì)占用內(nèi)存,我在這里不用多說,有興趣的查閱相關(guān)資料吧,大體是這樣.我們是把數(shù)據(jù)寫入一個(gè)自定義的application里面,在制定的時(shí)間讀取刷新的,大體思路就是這樣.
實(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ù)沒有顯示完畢時(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>" '存在數(shù)據(jù),接著賦值 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大顯身手了:
'該函數(shù)是寫入緩存 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í)間過去了沒 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 ' 這里隨便寫上了記錄的緩存的過去時(shí)間,相對(duì)總秒數(shù)倒差 : Response.Write "<b><font color=""red"">刷新緩存顯示 ..." Response.Write "</font></b><br><br>" End If End Function %>
說明完畢.
以下為完整無注釋代碼
<% Function DisplayRecords() Dim sql, conn, rs sql = "SELECT id, [szd_f], [szd_t] FROM admin" 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 If Not rs.EOF Then 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>" DisplayRecords = temp Else DisplayRecords = "Data Not Available." End If rs.Close conn.Close Set rs = Nothing Set conn = Nothing End Function
Function DisplayCachedRecords(Secs) Dim retVal, datVal, temp1 retVal = Application("cache_demo") datVal = Application("cache_demo_date") If datVal = "" Then datVal = DateAdd("s",Secs,Now) End If temp1 = DateDiff("s", Now, datVal) If temp1 > 0 And retVal <> "" Then DisplayCachedRecords = retVal Response.Write "<b><font color=""green"">利用緩存讀取數(shù)據(jù)" Response.Write " ... (" & temp1 & " 秒剩余)</font></b>" Response.Write "<br><br>" Else Dim temp2 temp2 = DisplayRecords() Application.Lock Application("cache_demo") = temp2 Application("cache_demo_date") = DateAdd("s",Secs,Now) Application.UnLock
DisplayCachedRecords = temp2 Response.Write "<b><font color=""red"">刷新緩存顯示 ..." Response.Write "</font></b><br><br>" End If End Function %>
調(diào)用方法:
<%=DisplayCachedRecords(20)%>
寫在后面的話:如果你感覺你的服務(wù)器內(nèi)存不夠大的話,不要大量使用緩存.
下載: cache.rar
出處:藍(lán)色理想
責(zé)任編輯:moby
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|