明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

asp提高首頁性能的一個(gè)技巧

[摘要]簡(jiǎn)單介紹:  一般一個(gè)網(wǎng)站的首頁訪問量是最大的,如果您的網(wǎng)站的首頁打開的非常緩慢,您的客戶將會(huì)陸續(xù)離開你的網(wǎng)站.通常我們把需要經(jīng)過復(fù)雜運(yùn)算或者查詢數(shù)據(jù)庫得出的數(shù)據(jù)緩存起來或者生成靜態(tài)網(wǎng)頁來提高web...

簡(jiǎn)單介紹:
  一般一個(gè)網(wǎng)站的首頁訪問量是最大的,如果您的網(wǎng)站的首頁打開的非常緩慢,您的客戶將會(huì)陸續(xù)離開你的網(wǎng)站.通常我們把需要經(jīng)過復(fù)雜運(yùn)算或者查詢數(shù)據(jù)庫得出的數(shù)據(jù)緩存起來或者生成靜態(tài)網(wǎng)頁來提高web應(yīng)用的性能,這次我們直接把首頁的輸出緩存成一個(gè)字符串,然后定時(shí)更新,即照顧了性能,又不影響首頁的時(shí)效性.這里用到了一些VBS自定義類,Application對(duì)象,XmlHttp對(duì)象,adodb.stream對(duì)象的一些東西,相關(guān)知識(shí)大家可以查資料了解.
  最好讓這個(gè)頁和你要緩存的頁在一個(gè)目錄下,要不有些相對(duì)路徑的圖片就無法顯示了,另外緩存有的頁面會(huì)出現(xiàn)亂碼,我還不知道怎么解決這個(gè)問題呢,可能在Response的時(shí)候需要設(shè)置一下編碼類型,大家可以試試

 

<%
Dim wawa,StarTime,EndTime
StarTime=Timer()
Set wawa=new Cls_Cache
wawa.Reloadtime=0.5
wawa.CacheName="wawa"
wawa.Name="XmlInfoIndex"
If wawa.ObjIsEmpty() Then CacheXmlInfoIndex()
Response.Write wawa.value
EndTime=Timer()
Response.Write "<br>執(zhí)行時(shí)間:" & FormatNumber((Endtime-StarTime)*1000,5) & "毫秒。"

Sub CacheXmlInfoIndex()
 Dim BodyText, xml
 Set xml = Server.CreateObject("Microsoft.XMLHTTP")
 '把下面的地址替換成你的首頁的文件地址,一定要用http://開頭的絕對(duì)路徑,不能寫相對(duì)路徑
 xml.Open "GET", "http://onlytiancai/bak/vote/InfoIndex.asp", False
 xml.Send 
 BodyText=xml.ResponseBody
 BodyText=BytesToBstr(BodyText,"gb2312")
 wawa.Value=BodyText
Set xml = Nothing
End Sub
Function BytesToBstr(body,Cset)
        dim objstream
        set objstream = Server.CreateObject("adodb.stream")
        objstream.Type = 1
        objstream.Mode =3
        objstream.Open
        objstream.Write body
        objstream.Position = 0
        objstream.Type = 2
        objstream.Charset = Cset
        BytesToBstr = objstream.ReadText
        objstream.Close
        set objstream = nothing
End Function
%>
<%
'下面這個(gè)類可以保存在單獨(dú)的文件里,然后包含到此頁
Class Cls_Cache
 Rem ==================使用說明==============================================
 Rem = 本類模塊是動(dòng)網(wǎng)先鋒原創(chuàng),作者:迷城浪子。如采用本類模塊,請(qǐng)不要去掉這個(gè)說明。這段注釋不會(huì)影響執(zhí)行的速度。=
 Rem = 作用:緩存和緩存管理類                                                                                  =
 Rem = 公有變量:Reloadtime 過期時(shí)間(單位為分鐘)缺省值為14400,                                               =
 Rem = MaxCount 緩存對(duì)象的最大值,超過則自動(dòng)刪除使用次數(shù)少的對(duì)象。缺省值為300                                  =
 Rem = CacheName 緩存組的總名稱,缺省值為"Dvbbs",如果一個(gè)站點(diǎn)中有超過一個(gè)緩存組,則需要外部改變這個(gè)值。        =
 Rem = 屬性:Name 定義緩存對(duì)象名稱,只寫屬性。                                                                  =
 Rem = 屬性:value 讀取和寫入緩存數(shù)據(jù)。                                                                         =
 Rem = 函數(shù):ObjIsEmpty()判斷當(dāng)前緩存是否過期。                                                                =
 Rem = 方法:DelCahe(MyCaheName)手工刪除一個(gè)緩存對(duì)象,參數(shù)是緩存對(duì)象的名稱。                                   =
 Rem ================================================================
 Public Reloadtime,MaxCount,CacheName
 Private LocalCacheName,CacheData,DelCount
 Private Sub Class_Initialize()
  Reloadtime=14400
  CacheName="Dvbbs"
 End Sub
 Private Sub SetCache(SetName,NewValue)
  Application.Lock
  Application(SetName) = NewValue
  Application.unLock
 End Sub
 Private Sub makeEmpty(SetName)
  Application.Lock
  Application(SetName) = Empty
  Application.unLock
 End Sub
 Public  Property Let Name(ByVal vNewValue)
  LocalCacheName=LCase(vNewValue)
 End Property
 Public  Property Let Value(ByVal vNewValue)
  If LocalCacheName<>"" Then
   CacheData=Application(CacheName&"_"&LocalCacheName)
   If IsArray(CacheData)  Then
    CacheData(0)=vNewValue
    CacheData(1)=Now()
   Else
    ReDim CacheData(2)
    CacheData(0)=vNewValue
    CacheData(1)=Now()
   End If
   SetCache CacheName&"_"&LocalCacheName,CacheData
  Else
   Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
  End If  
 End Property
 Public Property Get Value()
  If LocalCacheName<>"" Then
   CacheData=Application(CacheName&"_"&LocalCacheName) 
   If IsArray(CacheData) Then
    Value=CacheData(0)
   Else
    Err.Raise vbObjectError + 1, "DvbbsCacheServer", " The CacheData Is Empty."
   End If
  Else
   Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
  End If
 End Property
 Public Function ObjIsEmpty()
  ObjIsEmpty=True
  CacheData=Application(CacheName&"_"&LocalCacheName)
  If Not IsArray(CacheData) Then Exit Function
  If Not IsDate(CacheData(1)) Then Exit Function
  If DateDiff("s",CDate(CacheData(1)),Now()) < 60*Reloadtime  Then
   ObjIsEmpty=False
  End If
 End Function
 Public Sub DelCahe(MyCaheName)
  makeEmpty(CacheName&"_"&MyCaheName)
 End Sub
End Class
%>