磁盘缓存类

2006-01-18 16:49:39

作者: 沙龙巴斯2005-6-29 8:42:49 类文件:mRsCache_Class.asp <% '*************mRsCache_Class.asp**************** Class mRsCache_Class '------------------------------------------------------------- '磁盘缓存类 mRsCache Ver 2.0 Build 20050628 '------------------------------------------------------------- '用途: ' 将记录集(RecordSet对象)保存在磁盘上,保存后其他页面可重新打开该记录集 ' 减少数据库读写次数,适用于数据库中不经常改变的数据 '类成员: ' (属性) ' Version 版本信息,只读 ' Name 缓存文件名称,绝对路径,例如C:\aaa.rst,只写 ' IsAvailable 缓存文件是否可用,只读 ' (方法) ' Add(rs_tmp) 将记录集rs_tmp保存到缓存文件中(缓存文件名称应先通过Name属性设定) ' Clear 清除缓存文件(缓存文件名称应先通过Name属性设定) '------------------------------------------------------------- '作者:午夜狂龙 Madpolice '联系:madpolice_dong@163.com '------------------------------------------------------------- Private pName '缓存文件名称,绝对路径,例如C:\aaa.rst Private pFso 'fso对象 Private pVersion '版本 Private pExpireHours '缓存多少小时后过期 Public Property Get Version() Version = pVersion End Property Public Property Let Name(ByVal str_tmp) pName = str_tmp End Property Public Property Get IsAvailable() Dim pRndMinutes,pExpireMinutes,pFile,pFileLastModifyTime If (pFso.FileExists(pName)) Then Randomize pRndMinutes = Int(9 * Rnd) + 1 '随机数字,避免所有缓存同时过期 pExpireMinutes = 60 * pExpireHours + pRndMinutes Set pFile = pFso.GetFile(pName) pFileLastModifyTime = pFile.DateLastModified Set pFile = Nothing If DateDiff("n",pFileLastModifyTime,Now()) >= pExpireMinutes Then IsAvailable = False Else IsAvailable = True End If Else IsAvailable = False End If End Property Public Sub Add(ByRef rs_tmp) Call Clear() rs_tmp.Save pName End Sub Public Sub Clear() If (pFso.FileExists(pName)) Then pFso.DeleteFile pName,True End If End Sub Private Sub Class_Initialize() pVersion = "磁盘缓存类 mRsCache Ver 2.0 Build 20050628" pExpireHours = 24 '默认过期时间为1天 Set pFso = Server.CreateObject("Scripting.FileSystemObject") End Sub Private Sub Class_Terminate() Set pFso = Nothing End Sub End Class %> 调用示例:test.asp <!-- #include virtual = "mRsCache_class.asp" --> <% Dim ConnStr,Conn ConnStr = "Provider=Microsoft.Jet.Oledb.4.0;" ConnStr = ConnStr & "Data Source=" & Server.Mappath("test.mdb") Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open ConnStr %> <% '==========================操作缓存类示例开始=========================== Dim mRsCache Set mRsCache = New mRsCache_Class '建立缓存对象 Dim rs Set rs = Server.CreateObject("ADODB.RecordSet") '建立记录集 Dim FileName FileName = Server.Mappath("someFile.rst") '缓存文件物理路径,文件名(包括扩展名)可执行定义 mRsCache.Name = FileName '设置缓存类的Name属性 If mRsCache.IsAvailable Then '如果缓存可用 rs.Open FileName '则加载缓存文件到记录集中 Else '否则 Dim sql '从数据库中读取内容 sql = "sel&#101;ct * from someTable" rs.Open sql,Conn,0,1 mRsCache.Add rs '将记录集加入缓存 End If Response.Write rs(0) '从记录集中读数据 rs.Close '关闭记录集 Set rs = Nothing '释放记录集对象 Set mRsCache = Nothing '释放缓存对象 '==========================操作缓存类示例结束=========================== %> <% Conn.Close Set Conn = Nothing %>