asp提高首頁性能的一個技巧
簡單介紹:一般一個網站的首頁訪問量是最大的,如果您的網站的首頁打開的非常緩慢,您的客戶將會陸續離開你的網站.通常我們把需要經過復雜運算或者查詢數據庫得出的數據緩存起來或者生成靜態網頁來提高web應用的性能,這次我們直接把首頁的輸出緩存成一個字符串,然后定時更新,即照顧了性能,又不影響首頁的時效性.這里用到了一些vbs自定義類,application對象,xmlhttp對象,adodb.stream對象的一些東西,相關知識大家可以查資料了解.
最好讓這個頁和你要緩存的頁在一個目錄下,要不有些相對路徑的圖片就無法顯示了,另外緩存有的頁面會出現亂碼,我還不知道怎么解決這個問題呢,可能在response的時候需要設置一下編碼類型,大家可以試試
<%
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 "
執行時間:" & formatnumber((endtime-startime)*1000,5) & "毫秒。"
sub cachexmlinfoindex()
dim bodytext, xml
set xml = server.createobject("microsoft.xmlhttp")
'把下面的地址替換成你的首頁的文件地址,一定要用http://開頭的絕對路徑,不能寫相對路徑
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
%>
<%
'下面這個類可以保存在單獨的文件里,然后包含到此頁
class cls_cache
rem ==================使用說明==============================================
rem = 本類模塊是動網先鋒原創,作者:迷城浪子。如采用本類模塊,請不要去掉這個說明。這段注釋不會影響執行的速度。=
rem = 作用:緩存和緩存管理類 =
rem = 公有變量:reloadtime 過期時間(單位為分鐘)缺省值為14400, =
rem = maxcount 緩存對象的最大值,超過則自動刪除使用次數少的對象。缺省值為300 =
rem = cachename 緩存組的總名稱,缺省值為"dvbbs",如果一個站點中有超過一個緩存組,則需要外部改變這個值。 =
rem = 屬性:name 定義緩存對象名稱,只寫屬性。 =
rem = 屬性:value 讀取和寫入緩存數據。 =
rem = 函數:objisempty()判斷當前緩存是否過期。 =
rem = 方法:delcahe(mycahename)手工刪除一個緩存對象,參數是緩存對象的名稱。 =
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
%>
最好讓這個頁和你要緩存的頁在一個目錄下,要不有些相對路徑的圖片就無法顯示了,另外緩存有的頁面會出現亂碼,我還不知道怎么解決這個問題呢,可能在response的時候需要設置一下編碼類型,大家可以試試
代碼如下:
<%
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 "
執行時間:" & formatnumber((endtime-startime)*1000,5) & "毫秒。"
sub cachexmlinfoindex()
dim bodytext, xml
set xml = server.createobject("microsoft.xmlhttp")
'把下面的地址替換成你的首頁的文件地址,一定要用http://開頭的絕對路徑,不能寫相對路徑
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
%>
<%
'下面這個類可以保存在單獨的文件里,然后包含到此頁
class cls_cache
rem ==================使用說明==============================================
rem = 本類模塊是動網先鋒原創,作者:迷城浪子。如采用本類模塊,請不要去掉這個說明。這段注釋不會影響執行的速度。=
rem = 作用:緩存和緩存管理類 =
rem = 公有變量:reloadtime 過期時間(單位為分鐘)缺省值為14400, =
rem = maxcount 緩存對象的最大值,超過則自動刪除使用次數少的對象。缺省值為300 =
rem = cachename 緩存組的總名稱,缺省值為"dvbbs",如果一個站點中有超過一個緩存組,則需要外部改變這個值。 =
rem = 屬性:name 定義緩存對象名稱,只寫屬性。 =
rem = 屬性:value 讀取和寫入緩存數據。 =
rem = 函數:objisempty()判斷當前緩存是否過期。 =
rem = 方法:delcahe(mycahename)手工刪除一個緩存對象,參數是緩存對象的名稱。 =
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
%>