cache 為了提高網站首頁的效能,首頁凡是需要調用資料庫顯示資料的地方都會先試圖從緩衝中調用資料,如果緩衝中沒有可用資料再開啟資料庫取出記錄集,為了讓頁面顯示資料和資料庫在一定時間內同步,我們把緩衝的到期時間設定成30秒。
聲明:緩衝管理類出自於動網論壇7.0
注意:最好不要在緩衝裡直接緩衝帶狀態的對象和MTA模式的對象,比如說直接緩衝記錄集或者資料庫連結化物件等。
<%
Sub ShowRsArr(rsArr)
'用表格顯示記錄集getrows產生的數組的表結構
'
Response.Write "<table width=100% border=0 cellspacing=0 cellpadding=0>"
If Not IsEmpty(rsArr) Then
For y=0 To Ubound(rsArr,2)
Response.Write"<tr>"
for x=0 to Ubound(rsArr,1)
Response.Write "<td>"&rsArr(x,y)&"</td>"
next
Response.Write"</tr>"
next
Else
Response.Write "<tr>"
Response.Write "<td colspan="&rs.fields.count-1&">No Records</td>"
Response.Write "</tr>"
End If
Response.Write "</table>"
End Sub
Class Cls_Cache
Rem ==================使用說明=================================================================================
Rem = 本類別模組是動網先鋒原創,作者:迷城浪子。如採用本類別模組,請不要去掉這個說明。這段注釋不會影響執行的速度。=
Rem = 作用:緩衝和緩衝管理類 =
Rem = 公有變數:Reloadtime 到期時間(單位為分鐘)預設值為14400, =
Rem = MaxCount 緩衝對象的最大值,超過則自動刪除使用次數少的對象。預設值為300 =
Rem = CacheName 緩衝組的總名稱,預設值為"Dvbbs",如果一個網站中有超過一個緩衝組,則需要外部改變這個值。 =
Rem = 屬性:Name 定義緩衝對象名稱,唯寫屬性。 =
Rem = 屬性:value 讀取和寫入快取資料。 =
Rem = 函數:ObjIsEmpty()判斷當前緩衝是否到期。 =
Rem = 方法:DelCahe(MyCaheName)手工刪除一個緩衝對象,參數是緩衝對象的名稱。 =
Rem ===========================================================================================================
Public Reloadtime,MaxCount,CacheName
Private LocalCacheName,CacheData,DelCount
Private Sub Class_Initialize()
Reloadtime=14400
CacheName="Dvbbs"
End Sub
Private Sub SetCache(SetName,NewValue)
Application.Lock
Application(SetName) = NewValue
Application.unLock
End Sub
Private Sub makeEmpty(SetName)
Application.Lock
Application(SetName) = Empty
Application.unLock
End Sub
Public Property Let Name(ByVal vNewValue)
LocalCacheName=LCase(vNewValue)
End Property
Public Property Let Value(ByVal vNewValue)
If LocalCacheName<>"" Then
CacheData=Application(CacheName&"_"&LocalCacheName)
If IsArray(CacheData) Then
CacheData(0)=vNewValue
CacheData(1)=Now()
Else
ReDim CacheData(2)
CacheData(0)=vNewValue
CacheData(1)=Now()
End If
SetCache CacheName&"_"&LocalCacheName,CacheData
Else
Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
End If
End Property
Public Property Get Value()
If LocalCacheName<>"" Then
CacheData=Application(CacheName&"_"&LocalCacheName)
If IsArray(CacheData) Then
Value=CacheData(0)
Else
Err.Raise vbObjectError + 1, "DvbbsCacheServer", " The CacheData Is Empty."
End If
Else
Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
End If
End Property
Public Function ObjIsEmpty()
ObjIsEmpty=True
CacheData=Application(CacheName&"_"&LocalCacheName)
If Not IsArray(CacheData) Then Exit Function
If Not IsDate(CacheData(1)) Then Exit Function
If DateDiff("s",CDate(CacheData(1)),Now()) < 60*Reloadtime Then
ObjIsEmpty=False
End If
End Function
Public Sub DelCahe(MyCaheName)
makeEmpty(CacheName&"_"&MyCaheName)
End Sub
End Class
Dim strconn,rs
strconn="Driver={sql server};server=localhost;database=northwind;uid=sa;pwd=sa;"
Public Function GetEmployees()
Dim SQL,Rs,Cache
Set Cache=New Cls_Cache
Cache.Reloadtime=0.5
Cache.CacheName="wawa"
Cache.Name="Employees"
If Cache.ObjIsEmpty() Then
Set rs=Server.CreateObject("ADODB.Recordset")
SQL = "select EmployeeID, LastName, FirstName from employees order by employeeid desc"
Rs.Open SQL,strconn,1,1
Cache.value = Rs.GetRows(5)
Rs.Close:Set Rs=Nothing
End If
GetEmployees=Cache.Value
Set Cache=Nothing
End Function
ShowRsArr(GetEmpLoyees)
%>
<script>
function TimeOut(a){
var c=a-1;
if(c==0) {
window.location.href=window.location;
}else{
document.all.abc.innerHTML="離緩衝結束還有:"+c+"秒";
window.setTimeout('TimeOut('+c+')',1000);
}
}
</script>
<body >
<div id="abc"></div>