在ASP中類比.NET下的cache技術

來源:互聯網
上載者:User
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>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.