asp开发中的XML缓存类

提高网站的访问速度的一个方法,就是减少程序对数据库的访问,可以采取使用xml作为临时数据库,存放小数量数据,提高查询速度    读取机制:自动判断有无缓存了的xml文件,当xml文件存在数据,侧从xml获取数据,反之从数据库读取;
    缓存机制:根据xml文件生存时间和用户自定义的缓存时间判断缓存有无过期,过期侧生存新的xml文件;
    有效减少数据库查询读取次数,缓存数据量小读取更快
    修改了一下,加了两个方法,使用更方便
    view sourceprint?001 <%
    002 Rem xml缓存类
    003 '--------------------------------------------------------------------
    004 '转载的时候请保留版权信息
    005 '作者:╰⑥月の雨╮
    006 '博客: http://chthp.cnblogs.com/
    007 '版本:ver1.0
    008 '本类部分借鉴 walkmanxml数据缓存类,使用更为方便 欢迎各位交流进步
    009 '--------------------------------------------------------------------
    010 Class XmlCacheCls
    011     Private m_DataConn                '数据源,必须已经打开
    012     Private m_CacheTime                '缓存时间,单位秒  默认10分钟
    013     Private m_XmlFile                'xml路径,用绝对地址,不需要加扩展名
    014     Private m_Sql                    'SQL语句
    015     Private m_SQLArr                '(只读)返回的数据数组
    016     Private m_ReadOn                '(只读)返回读取方式 1-数据库 2-xml 检测用
    017
    018     '类的属性=========================================
    019
    020     '数据源
    021     Public Property Set  Conn(v)
    022         Set m_DataConn = v
    023     End Property
    024     Public Property Get  Conn
    025         Conn = m_DataConn
    026     End Property
    027
    028     '缓存时间
    029     Public Property Let  CacheTime(v)
    030         m_CacheTime = v
    031     End Property
    032     Public Property Get  CacheTime
    033         CacheTime = m_CacheTime
    034     End Property
    035
    036     'xml路径,用绝对地址
    037     Public Property Let  XmlFile(v)
    038         m_XmlFile = v
    039     End Property
    040     Public Property Get  XmlFile
    041         XmlFile = m_XmlFile
    042     End Property
    043
    044     'Sql语句
    045     Public Property Let  Sql(v)
    046         m_Sql = v
    047     End Property
    048     Public Property Get  Sql
    049         Sql = m_Sql
    050     End Property
    051     '返回记录数组
    052     Public Property Get  SQLArr
    053         SQLArr = m_SQLArr
    054     End Property
    055
    056     '返回读取方式
    057     Public Property Get  ReadOn
    058         ReadOn = m_ReadOn
    059     End Property
    060
    061     '类的析构=========================================
    062
    063     Private Sub Class_Initialize()    '初始化类
    064         m_CacheTime=60*10     '默认缓存时间为10分钟
    065     End Sub
    066
    067     Private Sub Class_Terminate()    '释放类
    068
    069     End Sub
    070
    071     '类的公共方法=========================================
    072
    073     Rem 读取数据
    074     Public Function ReadData
    075         If FSOExistsFile(m_XmlFile) Then    '存在xml缓存,直接从xml中读取
    076             ReadDataFromXml
    077             m_ReadOn=2
    078         Else
    079             ReadDataFromDB
    080             m_ReadOn=1
    081         End If
    082     End Function
    083
    084     Rem 写入XML数据
    085     Public Function WriteDataToXml
    086         If FSOExistsFile(m_XmlFile) Then   '如果xml未过期则直接退出
    087             If Not isXmlCacheExpired(m_XmlFile,m_CacheTime) Then  Exit Function
    088         End If
    089         Dim rs
    090         Dim xmlcontent
    091         Dim k
    092         xmlcontent = ""
    093         xmlcontent = xmlcontent & "<?xml version=""1.0"" encoding=""gb2312""?>" & vbnewline
    094         xmlcontent = xmlcontent & " <root>" & vbnewline
    095         k=0
    096         Set Rs = Server.CreateObject("Adodb.Recordset")
    097         Rs.open m_sql,m_DataConn,1
    098         While Not rs.eof
    099             xmlcontent = xmlcontent & "  <item "
    100             For Each field In rs.Fields
    101                 xmlcontent = xmlcontent & field.name & "=""" & XMLStringEnCode(field.value) & """ "
    102             Next
    103             rs.movenext
    104             k=k+1
    105             xmlcontent = xmlcontent &  "></item>" & vbnewline
    106         Wend
    107         rs.close
    108         Set rs = Nothing
    109         xmlcontent = xmlcontent & " </root>" & vbnewline
    110
    111         Dim folderpath
    112         folderpath = Trim(left(m_XmlFile,InstrRev(m_XmlFile,"\")-1))
    113         Call CreateDIR(folderpath&"") '创建文件夹
    114         WriteStringToXMLFile m_XmlFile,xmlcontent
    115     End Function
    116
    117     '类的私有方法=========================================
    118
    119     Rem 从Xml文件读取数据
    120     Private Function ReadDataFromXml
    121         Dim SQLARR()            '数组
    122         Dim XmlDoc              'XmlDoc对象
    123         Dim objNode             '子节点
    124         Dim ItemsLength         '子节点的长度
    125         Dim AttributesLength    '子节点属性的长度
    126         Set XmlDoc=Server.CreateObject("Microsoft.XMLDOM")
    127         XmlDoc.Async=False
    128         XmlDoc.Load(m_XmlFile)
    129         Set objNode=XmlDoc.documentElement  '获取根节点
    130         ItemsLength=objNode.ChildNodes.length  '获取子节点的长度
    131         For items_i=0 To ItemsLength-1
    132             AttributesLength=objNode.childNodes(items_i)。Attributes.length '获取子节点属性的长度
    133             For Attributes_i=0 To AttributesLength-1
    134                 ReDim Preserve SQLARR(AttributesLength-1,items_i)
    135                 SQLArr(Attributes_i,items_i) = objNode.childNodes(items_i)。Attributes(Attributes_i)。Nodevalue