|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
使用filesystemobject,可以对服务器上的文件进行操作,浏览、复制、移动、删除等。有ado的支持,asp对数据库的操作非常得心应手。你甚至可以像使用本地数据库那样,管理远程主机上的数据库,对表格、记录进行各种操作。<P>打造一个高功能不乱的web站点一向是站长和程序员的妄想,本文用作者的一次亲自履历的来讲说怎样使用xml缓存手艺完成站点的高功能。我是从往年入手下手做138手机主题网的,接纳SQL2000做为数据库,开辟言语用的是Asp,查询的时分都是静态查询,间接用like%的体例,谁人时分归正一天的会见量小,同时在线的时分也就几十团体罢了,以是服务器也就可以胜任请求,跟着会见量渐渐增添,当同时在线到达几百人时,此时服务器入手下手不胜重负,CPU经常到达100%不降,网页翻开速率也超等慢,一个查询页面必要几秒钟乃至更长,因而我入手下手思索优化程序和数据库,数据库创建索引,不是很幻想,由于用的是like%这类体例,因而我想到了缓存,而xml自己的特性决意了他十分合适做数据库的缓存,好器材不敢独享,特公布出来,以便偕行交换,配合前进。
完成的思绪是如许的:程序读失信息时,先判别是不是缓存了xml数据,假如有,则间接从xml中读失信息,不然从数据库中读取,并将此次了局天生xml文件,以便今后挪用,加速速率,同时判别xml缓存文件是不是过时,假如过时则必要从头天生xml。上面是详细的代码。
xmlcachecls.asp
<%
Remxml数据缓存类
--------------------------------------------------
转载的时分请保存版权信息
作者:walkman
网址:手机主题http://www.shouji138.com
版本:ver1.0
接待列位交换前进
--------------------------------------------------
ClassXmlCacheCls
Rem公有变量界说
Privatem_CacheTime缓存工夫,单元秒
Privatem_PageSize每页巨细
Privatem_CachePageNumxml缓存页巨细
Privatem_XmlFilexml路径,用相对地点,不必要加扩大名
Privatem_SqlSQL语句
Privatem_TableName表名大概视图名
Privatem_Columns列名用,离隔
Privatem_CurPage以后页
Privatem_CacheType缓存范例:1,列表2,概况
Privatem_DataConn数据源,必需已翻开
Privatem_QueryType查询范例:1,间接用sql2,用存储历程
Privatem_SQLArr前往的数据数组
Privatem_RecordCount
Rem大众属性
缓存工夫
PublicPropertyLetCacheTime(v)
m_CacheTime=v
EndProperty
PublicPropertyGetCacheTime
CacheTime=m_CacheTime
EndProperty
每页巨细
PublicPropertyLetPageSize(v)
m_PageSize=v
EndProperty
PublicPropertyGetPageSize
PageSize=m_PageSize
EndProperty
xml缓存页巨细
PublicPropertyLetCachePageNum(v)
m_CachePageNum=v
EndProperty
PublicPropertyGetCachePageNum
CachePageNum=m_CachePageNum
EndProperty
xml路径,用相对地点
PublicPropertyLetXmlFile(v)
m_XmlFile=v
EndProperty
PublicPropertyGetXmlFile
XmlFile=m_XmlFile
EndProperty
xml路径,用相对地点
PublicPropertyLetSql(v)
m_Sql=v
EndProperty
PublicPropertyGetSql
Sql=m_Sql
EndProperty
表名大概视图名
PublicPropertyLetTableName(v)
m_TableName=v
EndProperty
PublicPropertyGetTableName
TableName=m_TableName
EndProperty
列名用,离隔
PublicPropertyLetColumns(v)
m_Columns=v
EndProperty
PublicPropertyGetColumns
Columns=m_Columns
EndProperty
以后页
PublicPropertyLetCurPage(v)
m_CurPage=v
EndProperty
PublicPropertyGetCurPage
CurPage=m_CurPage
EndProperty
缓存范例:1,列表2,概况
PublicPropertyLetCacheType(v)
m_CacheType=v
EndProperty
PublicPropertyGetCacheType
CacheType=m_CacheType
EndProperty
缓存范例:1,列表2,概况
PublicPropertySetConn(v)
Setm_DataConn=v
EndProperty
PublicPropertyGetConn
Conn=m_DataConn
EndProperty
前往纪录总数
PublicPropertyGetRecordCount
RecordCount=m_RecordCount
EndProperty
前往纪录数组
PublicPropertyGetSQLArr
SQLArr=m_SQLArr
EndProperty
Rem大众办法读取数据
PublicFunctionReadData
Ifm_CacheType=1Then
ReadListAndSearchData
Else
ReadContentData
EndIf
EndFunction
Rem读取概况信息
PrivateFunctionReadContentData
Dimxmlfile
xmlfile=m_XmlFile
IfFSOExistsFile(xmlfile)Then存在xml缓存,间接从xml中读取
ReadContentDataFromXmlxmlfile
Else
ReadContentDataFromDB
EndIf
EndFunction
Rem从xml文件读取概况信息
PrivateFunctionReadContentDataFromXml(xmlfile)
DimSQLARR()
DimXmlDoc
SetXmlDoc=Server.CreateObject("msxml2.FreeThreadedDOMDocument.3.0")
XmlDoc.Loadxmlfile
Dimitemslength,itemsi
itemslength=XmlDoc.documentElement.childNodes.length
Foritemsi=0Toitemslength-1
ReDimPreserveSQLARR(itemslength-1,0)
SQLARR(itemsi,0)=XmlDoc.documentElement.childNodes(itemsi).text
Next
SetXmlDoc=Nothing
m_SQLArr=SQLArr
EndFunction
Rem从Db中读取概况信息
PrivateFunctionReadContentDataFromDB()
Dimrs
DimSQLARR
Setrs=m_DataConn.execute(m_sql)
IFNotRs.eofThen
SQLArr=Rs.GetRows(1)
rs.close
Setrs=Nothing
Else
rs.close
Setrs=Nothing
ExitFunction
EndIf
m_SQLArr=SQLArr
EndFunction
Rem读取列表数据
PrivateFunctionReadListAndSearchData
DimsPagesize,TotalPage,CurPage,TotalRec
sPagesize=m_PageSize*m_CachePageNum
m_CurPage=CLng(m_CurPage)
Ifm_CurPageModm_CachePageNum=0Then
CurPage=m_CurPage/m_CachePageNum
Else
CurPage=int(clng(m_CurPage)/m_CachePageNum)+1
EndIf
Dimxmlfile
xmlfile=getXmlFileName(CurPage)
IfFSOExistsFile(xmlfile)Then存在xml缓存,间接从xml中读取
ReadListAndSearchDataFromXmlxmlfile
Else
ReadListAndSearchDataFromDB
EndIf
EndFunction
Rem从xml中读列表数据
PrivateFunctionReadListAndSearchDataFromXml(xmlfile)
DimSQLARR()
DimXmlDoc
SetXmlDoc=Server.CreateObject("msxml2.FreeThreadedDOMDocument.3.0")
XmlDoc.Loadxmlfile
Dimtotalrecont
totalrecont=XmlDoc.documentElement.selectSingleNode("totalrec").text
m_RecordCount=totalrecont
DimTotalRec
TotalRec=m_RecordCount
Iftotalrecont=0Then
SetXmlDoc=Nothing
m_SQLArr=SQLARR
ExitFunction
EndIf
DimTotalPage,curpage
curpage=m_CurPage
Ifm_CurPageModm_CachePageNum=0Then
CurPage=m_CurPage/m_CachePageNum
Else
CurPage=int(clng(m_CurPage)/m_CachePageNum)+1
EndIf
IfTotalRecModm_CachePageNum=0Then
TotalPage=totalrecont/m_CachePageNum
Else
TotalPage=int(clng(totalrecont)/m_CachePageNum)+1
EndIf
Ifcurpage>TotalPageThencurpage=TotalPage
Dimstarti
Dimstartn
startn=m_curpage-(curpage-1)*m_CachePageNum
Rem盘算入手下手地位
starti=(startn-1)*m_pagesize
Dimitems,item
Setitems=XmlDoc.documentElement.SelectNodes("item")
Dimi
Dimnum
Dimlength
length=items.length
num=0
Fori=startiTom_PageSize+starti-1
Ifi>=lengthThenExitFor
Setitem=items(i)
Dimattrlength
attrlength=item.attributes.length
ReDimPreserveSQLARR(attrlength,num)
DimAttribute
DimAttributei
Attributei=0
ForAttributei=0Toattrlength-1
SQLArr(Attributei,num)=item.attributes(Attributei).Nodevalue
Next
num=num+1
Next
SetXmlDoc=Nothing
m_SQLArr=SQLArr
EndFunction
Rem从DB中读列表数据
PrivateFunctionReadListAndSearchDataFromDB
Dimrs,TotalRec,CurPage
CurPage=m_CurPage
SetRs=Server.CreateObject("Adodb.Recordset")
Rs.openm_sql,m_DataConn,1
TotalRec=rs.recordcount
m_RecordCount=TotalRec
rs.pagesize=m_PageSize
IfCurPage>rs.PageCountThenCurPage=rs.PageCount
IfNotrs.eofThenrs.absolutePage=m_CurPage
DimSQLARR()
Dimk
k=0
WhileNotrs.eofandk<m_PageSize
Dimfieldlegth
fieldlegth=rs.Fields.count
ReDimPreserveSQLARR(fieldlegth,k)
Dimfieldi
Forfieldi=0Tofieldlegth-1
SQLArr(fieldi,k)=rs.Fields(fieldi).value
Next
rs.movenext
k=k+1
Wend
rs.close
Setrs=Nothing
m_SQLArr=SQLArr
EndFunction
Rem猎取xml文件称号
PrivateFunctiongetXmlFileName(num)
Dimtmpstr
tmpstr=LCase(m_XmlFile)
IfRight(tmpstr,4)=".xml"Then
tmpstr=Left(tmpstr,Len(tmpstr)-Len(".xml"))
EndIf
tmpstr=Replace(tmpstr,"%","_")
tmpstr=tmpstr&"_"&num&".xml"
getXmlFileName=tmpstr
EndFunction
Rem大众办法将数据写进xml文件
PublicFunctionWriteDataToXml
Ifm_CacheType=1Then
WriteListAndSearchDataToXml
Else
WriteContentDataToXml
EndIf
EndFunction
Rem写详细某条信息的概况xml
PrivateFunctionWriteContentDataToXml
Remxml未过时则间接加入
Dimxmlfile
xmlfile=m_XmlFile
IfFSOExistsFile(xmlfile)Then
IfNotisXmlCacheExpired(xmlfile,m_CacheTime)ThenExitFunction
EndIf
Dimrs
Setrs=Server.CreateObject("Adodb.Recordset")
Rs.openm_sql,m_DataConn
CreateContentXmlFilexmlfile,Rs
EndFunction
Rem列表和搜刮xml数据
PrivateFunctionWriteListAndSearchDataToXml
DimsPagesize,TotalPage,CurPage,TotalRec
sPagesize=m_PageSize*m_CachePageNum
m_CurPage=CLng(m_CurPage)
Ifm_CurPageModm_CachePageNum=0Then
CurPage=m_CurPage/m_CachePageNum
Else
CurPage=int(clng(m_CurPage)/m_CachePageNum)+1
EndIf
Dimxmlfile
xmlfile=getXmlFileName(CurPage)
Rem假如xml未过时则间接加入
IfFSOExistsFile(xmlfile)Then
IfNotisXmlCacheExpired(xmlfile,m_CacheTime)ThenExitFunction
EndIf
Dimrs
SetRs=Server.CreateObject("Adodb.Recordset")
Rs.openm_sql,m_DataConn,1
TotalRec=rs.recordcount
rs.pagesize=sPagesize
IfCurPage>rs.PageCountThenCurPage=rs.PageCount
CreateListAndSearchXMLFilexmlfile,TotalRec,Rs,sPagesize
EndFunction
Rem公有办法
Rem失掉文件的最初修正工夫
PrivateFunctionFSOGetFileLastModifiedTime(file)
Dimfso,f,s
Setfso=CreateObject("Scripting.FileSystemObject")
Setf=fso.GetFile(file)
FSOGetFileLastModifiedTime=f.DateLastModified
Setf=Nothing
Setfso=Nothing
EndFunction
Rem判别xml缓存是不是到期
PrivateFunctionisXmlCacheExpired(file,seconds)
Dimfilelasttime
filelasttime=FSOGetFileLastModifiedTime(file)
IfDateAdd("s",seconds,filelasttime)<NowThen
isXmlCacheExpired=True
Else
isXmlCacheExpired=False
EndIf
EndFunction
Rem文件是不是存在
PrivateFunctionFSOExistsFile(file)
Dimfso
Setfso=Server.CreateObject("Scripting.FileSystemObject")
Iffso.FileExists(file)Then
FSOExistsFile=true
Else
FSOExistsFile=false
EndIf
Setfso=nothing
EndFunction
Rem天生具体数据的xml
PrivateFunctionCreateContentXmlFile(xmlfile,Rs)
Dimxmlcontent
xmlcontent="<?xmlversion=""1.0""encoding=""gb2312""?>"&vbnewline
xmlcontent=xmlcontent&"<root>"&vbnewline
Dimfield
ForEachfieldInrs.Fields
xmlcontent=xmlcontent&"<"&field.name&">"
Dimvalue
value=field.value
IfTypeName(value)="String"Then
xmlcontent=xmlcontent&"<![CDATA["&Trim(value)&"]]>"
Else
xmlcontent=xmlcontent&Trim(value)
EndIf
xmlcontent=xmlcontent&"</"&field.name&">"&vbnewline
Next
rs.close
Setrs=Nothing
xmlcontent=xmlcontent&"</root>"&vbnewline
Dimfolderpath
folderpath=Trim(left(xmlfile,InstrRev(xmlfile,"")-1))
CallCreateDIR(folderpath&"")创立文件夹
WriteStringToXMLFilexmlfile,xmlcontent
EndFunction
Rem天生列表的xml
PrivateFunctionCreateListAndSearchXMLFile(xmlfile,TotalRec,Rs,sPagesize)
Dimxmlcontent
xmlcontent=""
xmlcontent=xmlcontent&"<?xmlversion=""1.0""encoding=""gb2312""?>"&vbnewline
xmlcontent=xmlcontent&"<root>"&vbnewline
xmlcontent=xmlcontent&"<totalrec>"&TotalRec&"</totalrec>"&vbnewline
Dimk
k=0
Dimfield
WhileNotrs.eofandk<sPagesize
xmlcontent=xmlcontent&"<item"
ForEachfieldInrs.Fields
xmlcontent=xmlcontent&field.name&"="""&XMLStringEnCode(field.value)&""""
Next
xmlcontent=xmlcontent&"></item>"&vbnewline
rs.movenext
k=k+1
Wend
rs.close
Setrs=Nothing
xmlcontent=xmlcontent&"</root>"&vbnewline
Dimfolderpath
folderpath=Trim(left(xmlfile,InstrRev(xmlfile,"")-1))
CallCreateDIR(folderpath&"")创立文件夹
WriteStringToXMLFilexmlfile,xmlcontent
EndFunction
Remxml本义字符
PrivateFunctionXMLStringEnCode(str)
Ifstr&""=""ThenXMLStringEnCode="":ExitFunction
str=Replace(str,"<","<")
str=Replace(str,">",">")
str=Replace(str,"","")
str=Replace(str,"""",""")
str=Replace(str,"&","&")
XMLStringEnCode=str
EndFunction
Rem写文件
PrivateSubWriteStringToXMLFile(filename,str)
OnErrorResumeNext
Dimfs,ts
Setfs=createobject("scripting.filesystemobject")
IfNotIsObject(fs)ThenExitSub
Setts=fs.OpenTextFile(filename,2,True)
ts.writeline(str)
ts.close
Setts=Nothing
Setfs=Nothing
EndSub
Rem创立文件夹
PrivatefunctionCreateDIR(byvalLocalPath)
OnErrorResumeNext
Dimi,FileObject,patharr,path_level,pathtmp,cpath
LocalPath=Replace(LocalPath,"","/")
SetFileObject=server.createobject("Scripting.FileSystemObject")
patharr=Split(LocalPath,"/")
path_level=UBound(patharr)
Fori=0Topath_level
Ifi=0Then
pathtmp=patharr(0)&"/"
Else
pathtmp=pathtmp&patharr(i)&"/"
EndIf
cpath=left(pathtmp,len(pathtmp)-1)
IfNotFileObject.FolderExists(cpath)Then
Response.writecpath
FileObject.CreateFoldercpath
EndIf
Next
SetFileObject=Nothing
Iferr.number0Then
CreateDIR=False
err.Clear
Else
CreateDIR=True
EndIf
EndFunction
EndClass
%>
此类包括两种缓存体例:一种是基于列表体例的,如依照某个种别显现信息、搜刮某个关头词举行显现;别的一种是具体页面的缓存,如显现详细的某篇文章。
此类与详细的营业逻辑有关,只卖力xml数据的读取和存储,判别是不是缓存过时决意是不是必要更新缓存。依照三层构架形式的话,它处于数据会见层。
asp可以使用微软的activeX使得网页功能无比强大,不过安全性也较差,而且是基于的windows服务器,所以性能稳定性也一般 |
|