ASP提高數(shù)據(jù)顯示效率-緩存探幽
更新時間:2008年04月13日 21:57:28 作者:
寫在前面的話:此篇還是asp相關(guān)的,相信玩ASP的都有這個感覺,當數(shù)據(jù)有5萬多條時-------just like音樂網(wǎng),要調(diào)用最新的10條在頁面顯示,糟糕的是,當n多用戶打開頁面訪問的時候,每個用戶每次都要讀取數(shù)據(jù)庫一次,這無疑降低了效率,很明顯, 如果能把數(shù)據(jù)能保存在內(nèi)存上,然后讀取,無疑加快了速度.
所謂緩存其實就是在內(nèi)存中開辟一個用來保存數(shù)據(jù)的空間,使用緩存你就不用頻繁的訪問你保存在硬盤上的數(shù)據(jù)了,因為這些數(shù)據(jù)我們希望每個用戶都能看到效果一 樣,考慮使用的是application對象,因為它是所有訪問者的共用的對象,存儲的信息和定義的事件能夠為所有者訪問者使用,這里要使用asp內(nèi)置對 象APPLICATION了,關(guān)于application,有2個方法[lock和unlock],2個集合[content和 staticobjects],2個事件[開始的application_onstart和application_end],application變 量不會因為用戶的離開而消失,一旦建立,一直等到網(wǎng)站關(guān)閉和程序卸載為止,正因為如此,使用的時候要特別小心!,否則會占用內(nèi)存,我在這里不用多說,有興 趣的查閱相關(guān)資料吧,大體是這樣.我們是把數(shù)據(jù)寫入一個自定義的application里面,在制定的時間讀取刷新的,大體思路就是這樣.
實例演示.先建立一個簡單的數(shù)據(jù)庫,寫個function讀取一下,寫入一個dim變量temp中:
以下是引用片段:
Function DisplayRecords()
'這個函數(shù)原來給一個變量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
'當符合sq語句l的數(shù)據(jù)沒有顯示完畢時
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)用的時候就是DisplayRecords.
下面是application大顯身手了:
'該函數(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 的值,也就是要計算時間過去了沒
If datVal = "" Then
'如果是空,datVal值為當前時間按秒加上secs定義的時間
datVal = DateAdd("s",Secs,Now)
End If
'temp1是判斷當前時間和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.------------------>重點
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"">刷新緩存顯示 ..."
Response.Write "</font></b><br><br>"
End If
End Function
%>
說明完畢.
以下為完整無注釋代碼
調(diào)用方法:<%=DisplayCachedRecords(20)%>
寫在后面的話:如果你感覺你的服務(wù)器內(nèi)存不夠大的話,不要大量使用緩存.
實例演示.先建立一個簡單的數(shù)據(jù)庫,寫個function讀取一下,寫入一個dim變量temp中:
以下是引用片段:
復(fù)制代碼 代碼如下:
Function DisplayRecords()
'這個函數(shù)原來給一個變量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
'當符合sq語句l的數(shù)據(jù)沒有顯示完畢時
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)用的時候就是DisplayRecords.
下面是application大顯身手了:
復(fù)制代碼 代碼如下:
'該函數(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 的值,也就是要計算時間過去了沒
If datVal = "" Then
'如果是空,datVal值為當前時間按秒加上secs定義的時間
datVal = DateAdd("s",Secs,Now)
End If
'temp1是判斷當前時間和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.------------------>重點
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"">刷新緩存顯示 ..."
Response.Write "</font></b><br><br>"
End If
End Function
%>
說明完畢.
以下為完整無注釋代碼
調(diào)用方法:<%=DisplayCachedRecords(20)%>
寫在后面的話:如果你感覺你的服務(wù)器內(nèi)存不夠大的話,不要大量使用緩存.
相關(guān)文章
ASP運行出錯:缺少對象: ''xmlDoc.documentElement''錯誤解決方法
ASP運行出錯:缺少對象: 'xmlDoc.documentElement'錯誤解決方法,需要的朋友可以參考下2012-03-03不用WinRar只有asp將網(wǎng)絡(luò)空間上的文件打包下載
非常不錯的asp代碼,此方法,不建議壓縮,大文件,一般的小文件壓幾個還很好用的2008-08-08asp createTextFile生成文本文件支持utf8
一般情況下可以使用fso的createTextFile函數(shù),但有時候我們需要生成utf8格式的文件,那么就可以用下面的函數(shù)擴展了2020-08-08一個能對訪問者進行編號、記錄訪問次數(shù)、IP、時間的統(tǒng)計制作實例
一個能對訪問者進行編號、記錄訪問次數(shù)、IP、時間的統(tǒng)計制作實例...2006-12-12