在ASP中模拟.NET下的cache技术

80酷酷网    80kuku.com

  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>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: