乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      實(shí)踐xml緩存技術(shù)構(gòu)建高性能web站點(diǎn)

       頤中八喜 2011-08-03
      實(shí)踐xml緩存技術(shù)構(gòu)建高性能web站點(diǎn)
      文章來源: 文章作者: 發(fā)布時間:2009-02-17


      打造一個高性能穩(wěn)定的web站點(diǎn)一直是站長和程序員的夢想,本文用作者的一次親身經(jīng)歷的來說說如何利用xml緩存技術(shù)實(shí)現(xiàn)站點(diǎn)的高性能。我是從今年開始做138手機(jī)主題網(wǎng)的,采用SQL2000做為數(shù)據(jù)庫,開發(fā)語言用的是Asp,查詢的時候都是動態(tài)查詢,直接用like %的方式,那個時候反正一天的訪問量小,同時在線的時候也就幾十個人而已,所以服務(wù)器也就能勝任要求,隨著訪問量慢慢增加,當(dāng)同時在線達(dá)到幾百人時,此時服務(wù)器開始不堪重負(fù),CPU常常達(dá)到100%不降,網(wǎng)頁打開速度也超級慢,一個查詢頁面需要幾秒鐘甚至更長,于是我開始考慮優(yōu)化程序和數(shù)據(jù)庫,數(shù)據(jù)庫建立索引,不是很理想,因為用的是like '% 這種方式,于是我想到了緩存,而xml本身的特點(diǎn)決定了他非常適合做數(shù)據(jù)庫的緩存,好東西不敢獨(dú)享,特發(fā)布出來,以便同行交流,共同進(jìn)步。
      實(shí)現(xiàn)的思路是這樣的:程序讀取信息時,先判斷是否緩存了xml數(shù)據(jù),如果有,則直接從xml中讀取信息,否則從數(shù)據(jù)庫中讀取,并將此次結(jié)果生成xml文件,以便以后調(diào)用,加快速度,同時判斷xml緩存文件是否過期,如果過期則需要重新生成xml。下面是具體的代碼。
      xmlcachecls.asp
      <%
      Rem xml數(shù)據(jù)緩存類
      '--------------------------------------------------
      '轉(zhuǎn)載的時候請保留版權(quán)信息
      '作者:walkman
      '網(wǎng)址:手機(jī)主題 http://www.
      '版本:ver1.0
      '歡迎各位交流進(jìn)步
      '--------------------------------------------------
      Class XmlCacheCls
      Rem 私有變量定義
      Private m_CacheTime '緩存時間,單位秒
      Private m_PageSize '每頁大小
      Private m_CachePageNum 'xml緩存頁大小
      Private m_XmlFile 'xml路徑,用絕對地址,不需要加擴(kuò)展名
      Private m_Sql 'SQL語句
      Private m_TableName '表名或者視圖名
      Private m_Columns '列名 用,隔開
      Private m_CurPage '當(dāng)前頁
      Private m_CacheType '緩存類型:1,列表 2,詳情
      Private m_DataConn '數(shù)據(jù)源,必須已經(jīng)打開
      Private m_QueryType '查詢類型:1,直接用sql 2,用存儲過程
      Private m_SQLArr '返回的數(shù)據(jù)數(shù)組
      Private m_RecordCount


      Rem 公共屬性
      '緩存時間
      Public Property Let CacheTime(v)
      m_CacheTime = v
      End Property
      Public Property Get CacheTime
      CacheTime = m_CacheTime
      End Property

      '每頁大小
      Public Property Let PageSize(v)
      m_PageSize = v
      End Property
      Public Property Get PageSize
      PageSize = m_PageSize
      End Property

      'xml緩存頁大小
      Public Property Let CachePageNum(v)
      m_CachePageNum = v
      End Property
      Public Property Get CachePageNum
      CachePageNum = m_CachePageNum
      End Property

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

      'xml路徑,用絕對地址
      'http://www./article.asp?typeid=2
      Public Property Let Sql(v)
      m_Sql = v
      End Property
      Public Property Get Sql
      Sql = m_Sql
      End Property

      '表名或者視圖名
      Public Property Let TableName(v)
      m_TableName = v
      End Property
      Public Property Get TableName
      TableName = m_TableName
      End Property

      '列名 用,隔開
      Public Property Let Columns(v)
      m_Columns = v
      End Property
      Public Property Get Columns
      Columns = m_Columns
      End Property

      '當(dāng)前頁
      Public Property Let CurPage(v)
      m_CurPage = v
      End Property
      Public Property Get CurPage
      CurPage = m_CurPage
      End Property


      '緩存類型:1,列表 2,詳情
      Public Property Let CacheType(v)
      m_CacheType = v
      End Property
      Public Property Get CacheType
      CacheType = m_CacheType
      End Property

      '緩存類型:1,列表 2,詳情
      Public Property Set Conn(v)
      Set m_DataConn = v
      End Property
      Public Property Get Conn
      Conn = m_DataConn
      End Property

      '返回記錄總數(shù)
      Public Property Get RecordCount
      RecordCount = m_RecordCount
      End Property
      '返回記錄數(shù)組
      'http://www./
      Public Property Get SQLArr
      SQLArr = m_SQLArr
      End Property

      Rem 公共方法 讀取數(shù)據(jù)
      Public Function ReadData
      If m_CacheType = 1 Then
      ReadListAndSearchData
      Else
      ReadContentData
      End If
      End Function

      Rem 讀取詳情信息
      Private Function ReadContentData
      Dim xmlfile
      xmlfile = m_XmlFile
      If FSOExistsFile(xmlfile) Then '存在xml緩存,直接從xml中讀取
      ReadContentDataFromXml xmlfile
      Else
      ReadContentDataFromDB
      End If
      End Function

      Rem 從xml文件讀取詳情信息
      Private Function ReadContentDataFromXml(xmlfile)
      Dim SQLARR()
      Dim XmlDoc
      Set XmlDoc = Server.CreateObject("msxml2.FreeThreadedDOMDocument.3.0")
      XmlDoc.Load xmlfile
      Dim itemslength,itemsi
      itemslength = XmlDoc.documentElement.childNodes.length
      For itemsi=0 To itemslength-1
      ReDim Preserve SQLARR(itemslength-1,0)
      SQLARR(itemsi,0) = XmlDoc.documentElement.childNodes(itemsi).text
      Next
      Set XmlDoc = Nothing
      m_SQLArr = SQLArr
      End Function

      Rem 從Db中讀取詳情信息
      Private Function ReadContentDataFromDB()
      Dim rs
      Dim SQLARR
      Set rs = m_DataConn.execute(m_sql)
      IF Not Rs.eof Then
      SQLArr=Rs.GetRows(1)
      rs.close
      Set rs = Nothing
      Else
      rs.close
      Set rs = Nothing
      Exit Function
      End If
      m_SQLArr = SQLArr
      End Function

      Rem 讀取列表數(shù)據(jù)
      Private Function ReadListAndSearchData
      Dim sPagesize,TotalPage,CurPage,TotalRec
      sPagesize = m_PageSize * m_CachePageNum
      m_CurPage = CLng(m_CurPage)

      If m_CurPage Mod m_CachePageNum = 0 Then
      CurPage = m_CurPage/m_CachePageNum
      Else
      CurPage = int(clng(m_CurPage)/m_CachePageNum) 1
      End If
      Dim xmlfile
      xmlfile = getXmlFileName(CurPage)
      If FSOExistsFile(xmlfile) Then '存在xml緩存,直接從xml中讀取
      ReadListAndSearchDataFromXml xmlfile
      Else
      ReadListAndSearchDataFromDB
      End If
      End Function
      Rem 從xml中讀列表數(shù)據(jù)
      Private Function ReadListAndSearchDataFromXml(xmlfile)
      Dim SQLARR()
      Dim XmlDoc
      Set XmlDoc = Server.CreateObject("msxml2.FreeThreadedDOMDocument.3.0")
      XmlDoc.Load xmlfile
      Dim totalrecont
      totalrecont = XmlDoc.documentElement.selectSingleNode("totalrec").text
      m_RecordCount = totalrecont
      Dim TotalRec
      TotalRec = m_RecordCount
      If totalrecont = 0 Then
      Set XmlDoc = Nothing
      m_SQLArr = SQLARR
      Exit Function
      End If
      Dim TotalPage,curpage
      curpage = m_CurPage
      If m_CurPage Mod m_CachePageNum = 0 Then
      CurPage = m_CurPage/m_CachePageNum
      Else
      CurPage = int(clng(m_CurPage)/m_CachePageNum) 1
      End If
      If TotalRec Mod m_CachePageNum =0 Then
      TotalPage = totalrecont/m_CachePageNum
      Else
      TotalPage = int(clng(totalrecont)/m_CachePageNum) 1
      End If

      If curpage>TotalPage Then curpage=TotalPage
      Dim starti
      Dim startn
      startn = m_curpage - (curpage-1) * m_CachePageNum
      Rem 計算開始位置
      starti = (startn-1) * m_pagesize
      Dim items,item
      Set items = XmlDoc.documentElement.SelectNodes("item")
      Dim i
      Dim num
      Dim length
      length = items.length
      num = 0
      For i = starti To m_PageSize starti -1
      If i >=length Then Exit For
      Set item = items(i)
      Dim attrlength
      attrlength = item.attributes.length
      ReDim Preserve SQLARR(attrlength,num)
      Dim Attribute
      Dim Attributei
      Attributei = 0
      For Attributei = 0 To attrlength-1
      SQLArr(Attributei,num) = item.attributes(Attributei).Nodevalue
      Next
      num = num 1
      Next
      Set XmlDoc = Nothing
      m_SQLArr = SQLArr
      End Function

      Rem 從DB中讀列表數(shù)據(jù)
      Private Function ReadListAndSearchDataFromDB
      Dim rs,TotalRec,CurPage
      CurPage = m_CurPage
      Set Rs = Server.CreateObject("Adodb.Recordset")
      Rs.open m_sql,m_DataConn,1
      TotalRec = rs.recordcount
      m_RecordCount = TotalRec
      rs.pagesize = m_PageSize
      If CurPage>rs.PageCount Then CurPage = rs.PageCount
      If Not rs.eof Then rs.absolutePage=m_CurPage
      Dim SQLARR()
      Dim k
      k = 0
      While Not rs.eof and k<m_PageSize
      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
      rs.close
      Set rs = Nothing
      m_SQLArr = SQLArr
      End Function

      Rem 獲取xml文件名稱
      Private Function getXmlFileName(num)
      Dim tmpstr
      tmpstr = LCase(m_XmlFile)
      If Right(tmpstr,4) = ".xml" Then
      tmpstr = Left(tmpstr,Len(tmpstr)-Len(".xml"))
      End If
      tmpstr = Replace(tmpstr,"%","_")
      tmpstr = tmpstr & "_" & num & ".xml"
      getXmlFileName = tmpstr
      End Function

      Rem 公共方法 將數(shù)據(jù)寫入xml文件
      Public Function WriteDataToXml
      If m_CacheType = 1 Then
      WriteListAndSearchDataToXml
      Else
      WriteContentDataToXml
      End If
      End Function

      Rem 寫具體某條信息的詳情xml
      Private Function WriteContentDataToXml
      Rem xml未過期則直接退出
      Dim xmlfile
      xmlfile = m_XmlFile
      If FSOExistsFile(xmlfile) Then
      If Not isXmlCacheExpired(xmlfile,m_CacheTime) Then Exit Function
      End If
      Dim rs
      Set rs = Server.CreateObject("Adodb.Recordset")
      Rs.open m_sql,m_DataConn
      CreateContentXmlFile xmlfile,Rs
      End Function

      Rem 列表和搜索xml數(shù)據(jù)
      Private Function WriteListAndSearchDataToXml

      Dim sPagesize,TotalPage,CurPage,TotalRec
      sPagesize = m_PageSize * m_CachePageNum
      m_CurPage = CLng(m_CurPage)

      If m_CurPage Mod m_CachePageNum = 0 Then
      CurPage = m_CurPage/m_CachePageNum
      Else
      CurPage = int(clng(m_CurPage)/m_CachePageNum) 1
      End If
      Dim xmlfile
      xmlfile = getXmlFileName(CurPage)
      Rem 如果xml未過期則直接退出
      If FSOExistsFile(xmlfile) Then
      If Not isXmlCacheExpired(xmlfile,m_CacheTime) Then Exit Function
      End If
      Dim rs
      Set Rs = Server.CreateObject("Adodb.Recordset")
      Rs.open m_sql,m_DataConn,1
      TotalRec = rs.recordcount
      rs.pagesize = sPagesize
      If CurPage>rs.PageCount Then CurPage = rs.PageCount
      CreateListAndSearchXMLFile xmlfile,TotalRec,Rs,sPagesize
      End Function

      Rem 私有方法
      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 判斷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 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 生成詳細(xì)數(shù)據(jù)的xml
      Private Function CreateContentXmlFile(xmlfile,Rs)
      Dim xmlcontent
      xmlcontent = "<?xml version=""1.0"" encoding=""gb2312""?>" & vbnewline
      xmlcontent = xmlcontent & "<root>" & vbnewline

      Dim field
      For Each field In rs.Fields
      xmlcontent = xmlcontent & "<"&field.name&">"
      Dim value
      value = field.value
      If TypeName(value) = "String" Then
      xmlcontent = xmlcontent & "<![CDATA[" & Trim(value) & "]]>"
      Else
      xmlcontent = xmlcontent & Trim(value)
      End If
      xmlcontent = xmlcontent & "</"&field.name&">" & vbnewline
      Next
      rs.close
      Set rs = Nothing
      xmlcontent = xmlcontent & "</root>" & vbnewline

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

      Rem 生成列表的xml
      Private Function CreateListAndSearchXMLFile(xmlfile,TotalRec,Rs,sPagesize)
      Dim xmlcontent
      xmlcontent = ""
      xmlcontent = xmlcontent & "<?xml version=""1.0"" encoding=""gb2312""?>" & vbnewline
      xmlcontent = xmlcontent & " <root>" & vbnewline
      xmlcontent = xmlcontent & " <totalrec>" & TotalRec & "</totalrec>" & vbnewline
      Dim k
      k = 0
      Dim field
      While Not rs.eof and k<sPagesize
      xmlcontent = xmlcontent & " <item "
      For Each field In rs.Fields
      xmlcontent = xmlcontent & field.name & "=""" & XMLStringEnCode(field.value) & """ "
      Next
      xmlcontent = xmlcontent & "></item>" & vbnewline
      rs.movenext
      k=k 1
      Wend
      rs.close
      Set rs = Nothing
      xmlcontent = xmlcontent & " </root>" & vbnewline
      Dim folderpath
      folderpath = Trim(left(xmlfile,InstrRev(xmlfile,"\")-1))
      Call CreateDIR(folderpath&"") '創(chuàng)建文件夾
      WriteStringToXMLFile xmlfile,xmlcontent
      End Function
      Rem xml轉(zhuǎn)義字符
      Private Function XMLStringEnCode(str)
      If str&"" = "" Then XMLStringEnCode="":Exit Function
      str = Replace(str,"<","<")
      str = Replace(str,">",">")
      str = Replace(str,"'","'")
      str = Replace(str,"""",""")
      str = Replace(str,"&","&")
      XMLStringEnCode = str
      End Function
      Rem 寫文件
      Private Sub WriteStringToXMLFile(filename,str)
      'On Error Resume Next
      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 創(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
      %>

      此類包含兩種緩存方式:一種是基于列表方式的,如按照某個類別顯示信息、搜索某個關(guān)鍵詞進(jìn)行顯示;另外一種是詳細(xì)頁面的緩存,如顯示具體的某篇文章。
      此類與具體的業(yè)務(wù)邏輯無關(guān),只負(fù)責(zé)xml數(shù)據(jù)的讀取和存儲,判斷是否緩存過期決定是否需要更新緩存。按照三層構(gòu)架模式的話,它處于數(shù)據(jù)訪問層。

      調(diào)用這個類的代碼:
      Business.asp
      <%
      Rem xml數(shù)據(jù)緩存類業(yè)務(wù)邏輯層代碼
      '--------------------------------------------------
      '轉(zhuǎn)載的時候請保留版權(quán)信息
      '作者:walkman
      '網(wǎng)址:手機(jī)主題 http://www.
      '版本:ver1.0
      '歡迎各位交流進(jìn)步
      '--------------------------------------------------
      Rem 根據(jù)classid取列表數(shù)據(jù)
      Function GetListarr(classid,curpage,PageSize,CachePageNum,ByRef RecordCount)
      openConn
      Dim sql
      sql = "select thmid,thmname,picfileurl,win_theme.adddate from win_theme where win_theme.ClassID="&classid&" order by thmid desc"
      Dim cache
      Set cache = new XmlCacheCls
      cache.PageSize = PageSize '每頁N條記錄
      cache.CachePageNum = CachePageNum '一個xml文件緩存M頁的數(shù)據(jù)量
      cache.XmlFile = Server.Mappath("xmlcache/classxml/list_"&classid&".xml")
      cache.Sql = sql
      cache.CurPage = curpage
      cache.CacheType = 1
      Set cache.Conn = conn
      cache.ReadData
      Dim SqlArr
      SQLArr = cache.SQLArr
      RecordCount = cache.RecordCount
      Set cache = Nothing
      GetListarr = SqlArr
      End Function

      Rem 根據(jù)classid生成xml緩存
      Function CreateListxml(classid,curpage,PageSize,CachePageNum,CacheTime)
      Dim sql
      sql = "select thmid,thmname,picfileurl,win_theme.adddate from win_theme where win_theme.ClassID="&classid&" order by thmid desc"
      Dim cache
      Set cache = new XmlCacheCls
      cache.CacheTime = CacheTime '緩存時間
      cache.PageSize = PageSize '每頁N條記錄
      cache.CachePageNum = CachePageNum '一個xml文件緩存M頁的數(shù)據(jù)量
      cache.XmlFile = Server.Mappath("xmlcache/classxml/list_"&classid&".xml")
      cache.Sql = sql
      cache.CurPage = curpage
      cache.CacheType = 1
      Set cache.Conn = conn
      cache.WriteDataToXml
      Set cache = Nothing
      End Function

      Rem 根據(jù)keyword取列表數(shù)據(jù)
      Function GetSearcharr(keyword,curpage,PageSize,CachePageNum,ByRef RecordCount)
      openConn
      Dim sql
      Dim sqlkey
      sqlkey = Replace(keyword,"'","")
      sql = "select thmid,thmname,picfileurl,win_theme.adddate from win_theme where ThmName like '%"&sqlkey&"%' or ThmRange like '%"&sqlkey&"%' or ThmInstro like '%"&sqlkey&"%' order by thmid desc"
      Dim cache
      Set cache = new XmlCacheCls
      cache.PageSize = PageSize '每頁N條記錄
      cache.CachePageNum = CachePageNum '一個xml文件緩存M頁的數(shù)據(jù)量
      cache.XmlFile = Server.Mappath("xmlcache/searchxml/list_"&Server.URlEncode(Replace(keyword,"'",""))&".xml")
      cache.Sql = sql
      cache.CurPage = curpage
      cache.CacheType = 1
      Set cache.Conn = conn
      cache.ReadData
      Dim SqlArr
      SQLArr = cache.SQLArr
      RecordCount = cache.RecordCount
      Set cache = Nothing
      GetSearcharr = SqlArr
      End Function

      Rem 根據(jù)keyword生成xml緩存
      Function CreateSearchxml(keyword,curpage,PageSize,CachePageNum,CacheTime)
      Dim sql
      Dim sqlkey
      sqlkey = Replace(keyword,"'","")
      sql = "select thmid,thmname,picfileurl,win_theme.adddate from win_theme where ThmName like '%"&sqlkey&"%' or ThmRange like '%"&sqlkey&"%' or ThmInstro like '%"&sqlkey&"%' order by thmid desc"
      Dim cache
      Set cache = new XmlCacheCls
      cache.CacheTime = CacheTime '緩存時間
      cache.PageSize = PageSize '每頁N條記錄
      cache.CachePageNum = CachePageNum '一個xml文件緩存M頁的數(shù)據(jù)量
      cache.XmlFile = Server.Mappath("xmlcache/searchxml/list_"&Server.URlEncode(Replace(keyword,"'",""))&".xml")
      cache.Sql = sql
      cache.CurPage = curpage
      cache.CacheType = 1
      Set cache.Conn = conn
      cache.WriteDataToXml
      Set cache = Nothing
      End Function
      Rem 根據(jù)classid取列表數(shù)據(jù)
      Function GetDetailarr(thmid)
      openConn
      Dim sql
      sql = "select a.thmid,a.thmname,a.classid,b.classname,a.picfileurl,a.thmver,a.thmsize,a.thminstro,a.thmrange,a.thmfileurl,a.adddate from win_theme a,Win_Classify b where a.classid=b.classid and a.thmid="&thmid&""
      Dim thmidmod
      thmidmod = thmid Mod 100

      Dim cache
      Set cache = new XmlCacheCls
      cache.XmlFile = Server.Mappath("xmlcache/detailxml/"&thmidmod&"/"&thmid&".xml")
      cache.Sql = sql
      cache.CacheType = 2
      Set cache.Conn = conn
      cache.ReadData
      Dim SqlArr
      SQLArr = cache.SQLArr
      Set cache = Nothing
      GetDetailarr = SqlArr
      End Function
      Rem 根據(jù)keyword生成xml緩存
      Function CreateDetailxml(thmid,CacheTime)
      Dim sql
      sql = "select a.thmid,a.thmname,a.classid,b.classname,a.picfileurl,a.thmver,a.thmsize,a.thminstro,a.thmrange,a.thmfileurl,a.adddate from win_theme a,Win_Classify b where a.classid=b.classid and a.thmid="&thmid&""
      Dim thmidmod
      thmidmod = thmid Mod 100
      Dim cache
      Set cache = new XmlCacheCls
      cache.CacheTime = CacheTime '緩存時間
      cache.XmlFile = Server.Mappath("xmlcache/detailxml/"&thmidmod&"/"&thmid&".xml")
      cache.Sql = sql
      cache.CacheType = 2
      Set cache.Conn = conn
      cache.WriteDataToXml
      Set cache = Nothing
      End Function

      Rem 檢測動態(tài)數(shù)組是否已分配
      Function ismalloc(a)
      On Error Resume Next
      Dim i
      i = UBound(a)
      If Err Then
      ismalloc = False
      Else
      ismalloc = True
      End If
      End Function

      Function showData(SQLArr)
      If Not ismalloc(SQLArr) Then Exit Function
      Dim i,k
      Dim num
      num = 0
      i = UBound(SQLArr,1)
      k = UBound(SQLArr,2)
      Dim m,n
      For m = 0 To k
      num = num 1
      %>
      <ul class="listbox" onMouseOver="overtb(this)" onMouseOut="outtb(this)">
      <li>
      <a title="<%=SQLArr(1,m)%>" href="detail.asp?id=<%=SQLArr(0,m)%>" target="_blank">
      <img height="140" alt="<%=SQLArr(1,m)%>" src="http://www.<%=SQLArr(2,m)%>" width="107" border="0"></a>
      </li>
      <li class="green bold">
      <a title="<%=SQLArr(1,m)%>" href="detail.asp?id=<%=SQLArr(0,m)%>" target="_blank">
      <%=walkgottopic(Trim(SQLArr(1,m)),18)%></a>
      </li>
      <li><%=DateValue(SQLArr(3,m))%></li>
      </ul>
      <%
      next
      End Function
      %>
      這個文件是業(yè)務(wù)邏輯層代碼,負(fù)責(zé)根據(jù)不同的業(yè)務(wù)邏輯來實(shí)現(xiàn)xml數(shù)據(jù)的讀取和寫入,并提供接口方法給web表現(xiàn)層調(diào)用。
      具體的調(diào)用代碼:
      list.asp
      只顯示相關(guān)代碼。
      <%
      。。。。。。
      Dim classid
      classid = Request("classid")
      If classid = "" Or (Not IsNumeric(classid)) Then Response.write "參數(shù)錯誤!":Response.End()
      classid = CLng(classid)
      Dim sPagesize,TotalPage,CurPage,TotalRec,CachePageNum
      sPagesize = 20
      CachePageNum = 10
      CurPage = Trim(Request("page"))
      IF CurPage="" Or (Not IsNumeric(CurPage)) Then
      CurPage=1
      Else
      CurPage=Clng(CurPage)
      End IF
      Dim myarr
      myarr = GetListarr(classid,CurPage,sPagesize,CachePageNum,TotalRec)
      '總頁數(shù)
      TotalPage = int(clng(TotalRec)/sPagesize*-1)*-1
      If Clng(TotalRec)>0 Then
      showData myarr
      End If
      ................
      %>
      最后在頁面最底部調(diào)用一個asp的script語句來更新xml緩存。
      <script type="text/javascript" src="setcache.asp?action=list&curpage=<%=curpage%>&classid=<%=classid%>"></script>

      setcache.asp
      相關(guān)代碼
      <%
      openconn
      Dim action
      action = Trim(Request("action"))&""
      Dim curpage
      curpage = Request("curpage")
      Dim classid
      Dim keyword
      Dim thmid
      If action = "list" Then
      classid = Request("classid")
      If classid="" Or (Not IsNumeric(classid)) Or curpage="" Or (Not IsNumeric(curpage)) Then
      Else
      CreateListxml CLng(classid),CLng(curpage),20,10,60 * 60 * 2 '創(chuàng)建分類的xml
      End If
      ElseIf action = "search" Then
      keyword = Trim(Request("keyword"))
      If keyword="" Then
      Else
      CreateSearchxml keyword,CLng(curpage),20,10,60 * 60 * 2 '創(chuàng)建搜索的xml
      End If
      ElseIf action = "detail" Then
      thmid = Request("id")
      If thmid="" Or (Not IsNumeric(thmid)) Then
      Else
      CreateDetailxml CLng(thmid),60 * 60 * 2 '創(chuàng)建詳情的xml
      End If
      End If
      Call Closeconn
      Response.write " "
      Response.End
      %>
      至此,核心代碼都分享出來了,實(shí)踐證明,通過這樣的方式,我的138手機(jī)主題網(wǎng)的服務(wù)器的CPU占用率和內(nèi)存占用率明顯下降,訪問速度也明顯提高,從以前的需要幾秒甚至10多秒,到現(xiàn)在只需要10幾毫秒。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多