佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > asp xml 緩存類

asp xml 緩存類

熱門標簽:科大訊飛語音識別系統(tǒng) Linux服務器 Mysql連接數(shù)設置 電子圍欄 團購網站 服務器配置 銀行業(yè)務 阿里云
復制代碼 代碼如下:

%
Rem xml緩存類
'--------------------------------------------------------------------
'轉載的時候請保留版權信息
'作者:╰⑥月の雨╮
'版本:ver1.0
'本類部分借鑒 walkmanxml數(shù)據(jù)緩存類,使用更為方便 歡迎各位交流進步
'--------------------------------------------------------------------
Class XmlCacheCls
Private m_DataConn '數(shù)據(jù)源,必須已經打開
Private m_CacheTime '緩存時間,單位秒 默認10分鐘
Private m_XmlFile 'xml路徑,用絕對地址,不需要加擴展名
Private m_Sql 'SQL語句
Private m_SQLArr '(只讀)返回的數(shù)據(jù)數(shù)組
Private m_ReadOn '(只讀)返回讀取方式 1-數(shù)據(jù)庫 2-xml 檢測用

'類的屬性=========================================

'數(shù)據(jù)源
Public Property Set Conn(v)
Set m_DataConn = v
End Property
Public Property Get Conn
Conn = m_DataConn
End Property

'緩存時間
Public Property Let CacheTime(v)
m_CacheTime = v
End Property
Public Property Get CacheTime
CacheTime = m_CacheTime
End Property

'xml路徑,用絕對地址
Public Property Let XmlFile(v)
m_XmlFile = v
End Property
Public Property Get XmlFile
XmlFile = m_XmlFile
End Property

'Sql語句
Public Property Let Sql(v)
m_Sql = v
End Property
Public Property Get Sql
Sql = m_Sql
End Property
'返回記錄數(shù)組
Public Property Get SQLArr
SQLArr = m_SQLArr
End Property

'返回讀取方式
Public Property Get ReadOn
ReadOn = m_ReadOn
End Property

'類的析構=========================================

Private Sub Class_Initialize() '初始化類
m_CacheTime=60*10 '默認緩存時間為10分鐘
End Sub

Private Sub Class_Terminate() '釋放類

End Sub

'類的公共方法=========================================

Rem 讀取數(shù)據(jù)
Public Function ReadData
If FSOExistsFile(m_XmlFile) Then '存在xml緩存,直接從xml中讀取
ReadDataFromXml
m_ReadOn=2
Else
ReadDataFromDB
m_ReadOn=1
End If
End Function

Rem 寫入XML數(shù)據(jù)
Public Function WriteDataToXml
If FSOExistsFile(m_XmlFile) Then '如果xml未過期則直接退出
If Not isXmlCacheExpired(m_XmlFile,m_CacheTime) Then Exit Function
End If
Dim rs
Dim xmlcontent
Dim k
xmlcontent = ""
xmlcontent = xmlcontent "?xml version=""1.0"" encoding=""gb2312""?>" vbnewline
xmlcontent = xmlcontent " root>" vbnewline
k=0
Set Rs = Server.CreateObject("Adodb.Recordset")
Rs.open m_sql,m_DataConn,1
While Not rs.eof
xmlcontent = xmlcontent " item "
For Each field In rs.Fields
xmlcontent = xmlcontent field.name "=""" XMLStringEnCode(field.value) """ "
Next
rs.movenext
k=k+1
xmlcontent = xmlcontent ">/item>" vbnewline
Wend
rs.close
Set rs = Nothing
xmlcontent = xmlcontent " /root>" vbnewline

Dim folderpath
folderpath = Trim(left(m_XmlFile,InstrRev(m_XmlFile,"\")-1))
Call CreateDIR(folderpath"") '創(chuàng)建文件夾
WriteStringToXMLFile m_XmlFile,xmlcontent
End Function

'類的私有方法=========================================

Rem 從Xml文件讀取數(shù)據(jù)
Private Function ReadDataFromXml
Dim SQLARR() '數(shù)組
Dim XmlDoc 'XmlDoc對象
Dim objNode '子節(jié)點
Dim ItemsLength '子節(jié)點的長度
Dim AttributesLength '子節(jié)點屬性的長度
Set XmlDoc=Server.CreateObject("Microsoft.XMLDOM")
XmlDoc.Async=False
XmlDoc.Load(m_XmlFile)
Set objNode=XmlDoc.documentElement '獲取根節(jié)點
ItemsLength=objNode.ChildNodes.length '獲取子節(jié)點的長度
For items_i=0 To ItemsLength-1
AttributesLength=objNode.childNodes(items_i).Attributes.length '獲取子節(jié)點屬性的長度
For Attributes_i=0 To AttributesLength-1
ReDim Preserve SQLARR(AttributesLength-1,items_i)
SQLArr(Attributes_i,items_i) = objNode.childNodes(items_i).Attributes(Attributes_i).Nodevalue
Next
Next
Set XmlDoc = Nothing
m_SQLArr = SQLARR
End Function

Rem 從數(shù)據(jù)庫讀取數(shù)據(jù)
Private Function ReadDataFromDB
Dim rs
Dim SQLARR()
Dim k
k=0
Set Rs = Server.CreateObject("Adodb.Recordset")
Rs.open m_sql,m_DataConn,1
If Not (rs.eof and rs.bof) Then
While Not rs.eof
Dim fieldlegth
fieldlegth = rs.Fields.count
ReDim Preserve SQLARR(fieldlegth,k)
Dim fieldi
For fieldi = 0 To fieldlegth-1
SQLArr(fieldi,k) = rs.Fields(fieldi).value
Next
rs.movenext
k=k+1
Wend
End If
rs.close
Set rs = Nothing
m_SQLArr = SQLArr
End Function

'類的輔助私有方法=========================================

Rem 寫xml文件
Private Sub WriteStringToXMLFile(filename,str)
Dim fs,ts
Set fs= createobject("scripting.filesystemobject")
If Not IsObject(fs) Then Exit Sub
Set ts=fs.OpenTextFile(filename,2,True)
ts.writeline(str)
ts.close
Set ts=Nothing
Set fs=Nothing
End Sub

Rem 判斷xml緩存是否到期
Private Function isXmlCacheExpired(file,seconds)
Dim filelasttime
filelasttime = FSOGetFileLastModifiedTime(file)
If DateAdd("s",seconds,filelasttime) Now Then
isXmlCacheExpired = True
Else
isXmlCacheExpired = False
End If
End Function

Rem 得到文件的最后修改時間
Private Function FSOGetFileLastModifiedTime(file)
Dim fso,f,s
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.GetFile(file)
FSOGetFileLastModifiedTime = f.DateLastModified
Set f = Nothing
Set fso = Nothing
End Function

Rem 文件是否存在
Public Function FSOExistsFile(file)
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
FSOExistsFile = true
Else
FSOExistsFile = false
End If
Set fso = nothing
End Function

Rem xml轉義字符
Private Function XMLStringEnCode(str)
If str"" = "" Then XMLStringEnCode="":Exit Function
str = Replace(str,"","lt;")
str = Replace(str,">","gt;")
str = Replace(str,"'","apos;")
str = Replace(str,"""","quot;")
str = Replace(str,"","")
XMLStringEnCode = str
End Function

Rem 創(chuàng)建文件夾
Private function CreateDIR(byval LocalPath)
On Error Resume Next
Dim i,FileObject,patharr,path_level,pathtmp,cpath
LocalPath = Replace(LocalPath,"\","/")
Set FileObject = server.createobject("Scripting.FileSystemObject")
patharr = Split(LocalPath,"/")
path_level = UBound (patharr)
For i = 0 To path_level
If i=0 Then
pathtmp=patharr(0) "/"
Else
pathtmp = pathtmp patharr(i) "/"
End If
cpath = left(pathtmp,len(pathtmp)-1)
If Not FileObject.FolderExists(cpath) Then
'Response.write cpath
FileObject.CreateFolder cpath
End If
Next
Set FileObject = Nothing
If err.number>0 Then
CreateDIR = False
err.Clear
Else
CreateDIR = True
End If
End Function
End Class
'設置緩存
Function SetCache(xmlFilePath,CacheTime,Conn,Sql)
set cache=new XmlCacheCls
Set cache.Conn=Conn
cache.XmlFile=xmlFilePath
cache.Sql=Sql
cache.CacheTime=CacheTime
cache.WriteDataToXml
Set cache = Nothing
End Function
'讀取緩存
Function ReadCache(xmlFilePath,Conn,Sql,ByRef ReadOn)
set cache=new XmlCacheCls
Set cache.Conn=conn
cache.XmlFile=xmlFilePath
cache.Sql=Sql
cache.ReadData
ReadCache=cache.SQLArr
ReadOn=cache.ReadOn
End Function
%>

使用方法:
1 緩存數(shù)據(jù)到xml
代碼:
復制代碼 代碼如下:

!--#include file="Conn.asp"-->
!--#include file="Xml.asp"-->
%
set cache=new XmlCacheCls
Set cache.Conn=conn
cache.XmlFile=Server.Mappath("xmlcache/index/Top.xml")
cache.Sql="select top 15 prod_id,prod_name,prod_uptime from tblProduction"
cache.WriteDataToXml
%>

2 讀取緩存數(shù)據(jù)
代碼:
復制代碼 代碼如下:

!--#include file="Conn.asp"-->
!--#include file="Xml.asp"-->
%
set cache=new XmlCacheCls
Set cache.Conn=conn
cache.XmlFile=Server.Mappath("xmlcache/index/Top.xml")
cache.Sql="select top 15 prod_id,prod_name,prod_uptime from tblProduction order by prod_id asc"
cache.ReadData
rsArray=cache.SQLArr
if isArray(rsArray) then
for i=0 to ubound(rsArray,2)
for j=0 to ubound(rsArray,1)
response.Write(rsArray(j,i)"br>br>")
next
next
end if
%>
緩存時間,單位秒 默認10分鐘;也可以自己設定 cache.CacheTime=60*30 30分鐘

標簽:蚌埠 江蘇 衡水 萍鄉(xiāng) 衢州 廣元 大理 棗莊

巨人網絡通訊聲明:本文標題《asp xml 緩存類》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    年辖:市辖区| 连山| 左权县| 碌曲县| 藁城市| 广灵县| 桦川县| 安吉县| 油尖旺区| 读书| 蕉岭县| 南汇区| 讷河市| 旬阳县| 土默特右旗| 隆林| 绍兴县| 马鞍山市| 内江市| 思茅市| 安图县| 乐清市| 定南县| 安丘市| 蓝山县| 镇雄县| 巨野县| 简阳市| 砚山县| 汉川市| 通榆县| 河东区| 龙山县| 虎林市| 湘潭市| 阿瓦提县| 中方县| 宜兰县| 车致| 博野县| 虹口区|