利用 cache 做對(duì)比靜態(tài)頁(yè)的網(wǎng)頁(yè)技術(shù)
更新時(shí)間:2007年10月27日 12:04:48 作者:
一直想寫一套生成靜態(tài)頁(yè)面的文章系統(tǒng) 但面對(duì)生成靜態(tài)后的一些復(fù)雜數(shù)據(jù)庫(kù)交互問(wèn)題。又望而卻步!
于是就想 有沒(méi)有 在不耽誤數(shù)據(jù)交互的情況下,而又能降低服務(wù)器負(fù)擔(dān)的方法呢!
一個(gè)網(wǎng)站,訪問(wèn)量最大的莫過(guò)于 首頁(yè) 和主欄目頁(yè)了。 其他的頁(yè)面 我可以不去想, 首頁(yè)和主欄目頁(yè) 在大流量下服務(wù)器改如何承擔(dān)呢。
根據(jù)我編程2年多來(lái)的總結(jié)經(jīng)驗(yàn)我想去了一下方法!
不生成靜態(tài)頁(yè) 并且降低服務(wù)器負(fù)擔(dān)!
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
'讀取遠(yuǎn)程文件的函數(shù)
Public Function readRemoteFile(RemoteDataUrl)
Dim XMLHttp
'On Error Resume Next
Set XMLHttp = Server.CreateObject("Microsoft.XMLHTTP")
With XMLHttp
.Open "Get", RemoteDataUrl, False
.Send
readRemoteFile = BytesToBstr(.responseBody, "UTF-8")
End With
Set XMLHttp = Nothing
End Function
'編碼轉(zhuǎn)換
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
Function tobody()
Dim dateVal
'先試圖訪問(wèn)緩存,看有沒(méi)有,或者過(guò)期沒(méi)有
dateVal = Application("defaultdate")
If dateVal = "" Then dateVal = DateAdd("s",1200,Now)
If Application("default") <> "" Then
If DateDiff("s", Now, dateVal) > 0 Then
'如果有,就從緩存讀取,對(duì)服務(wù)器來(lái)說(shuō),就是從內(nèi)存讀取
tobody = Application("default")&"<!--new cache"&dateVal&"-->"
Exit Function
End If
End If
Dim body
'如果緩存沒(méi)有,則從遠(yuǎn)程讀取,并寫入緩存,設(shè)置緩存時(shí)間。
body = readRemoteFile("http://www.aoaob.com/default.asp")
tobody = body&"<!--made cache"&Now&"-->"
Application.Lock
Application("default") = body
Application("defaultdate") = DateAdd("s",1200,Now)
Application.UnLock
End Function
Response.Write(tobody())
%>
于是就想 有沒(méi)有 在不耽誤數(shù)據(jù)交互的情況下,而又能降低服務(wù)器負(fù)擔(dān)的方法呢!
一個(gè)網(wǎng)站,訪問(wèn)量最大的莫過(guò)于 首頁(yè) 和主欄目頁(yè)了。 其他的頁(yè)面 我可以不去想, 首頁(yè)和主欄目頁(yè) 在大流量下服務(wù)器改如何承擔(dān)呢。
根據(jù)我編程2年多來(lái)的總結(jié)經(jīng)驗(yàn)我想去了一下方法!
不生成靜態(tài)頁(yè) 并且降低服務(wù)器負(fù)擔(dān)!
復(fù)制代碼 代碼如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
'讀取遠(yuǎn)程文件的函數(shù)
Public Function readRemoteFile(RemoteDataUrl)
Dim XMLHttp
'On Error Resume Next
Set XMLHttp = Server.CreateObject("Microsoft.XMLHTTP")
With XMLHttp
.Open "Get", RemoteDataUrl, False
.Send
readRemoteFile = BytesToBstr(.responseBody, "UTF-8")
End With
Set XMLHttp = Nothing
End Function
'編碼轉(zhuǎn)換
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
Function tobody()
Dim dateVal
'先試圖訪問(wèn)緩存,看有沒(méi)有,或者過(guò)期沒(méi)有
dateVal = Application("defaultdate")
If dateVal = "" Then dateVal = DateAdd("s",1200,Now)
If Application("default") <> "" Then
If DateDiff("s", Now, dateVal) > 0 Then
'如果有,就從緩存讀取,對(duì)服務(wù)器來(lái)說(shuō),就是從內(nèi)存讀取
tobody = Application("default")&"<!--new cache"&dateVal&"-->"
Exit Function
End If
End If
Dim body
'如果緩存沒(méi)有,則從遠(yuǎn)程讀取,并寫入緩存,設(shè)置緩存時(shí)間。
body = readRemoteFile("http://www.aoaob.com/default.asp")
tobody = body&"<!--made cache"&Now&"-->"
Application.Lock
Application("default") = body
Application("defaultdate") = DateAdd("s",1200,Now)
Application.UnLock
End Function
Response.Write(tobody())
%>
相關(guān)文章
在asp中通過(guò)vbs類實(shí)現(xiàn)rsa加密與解密的代碼
在asp中通過(guò)vbs類實(shí)現(xiàn)rsa加密與解密的代碼...2007-03-03UpdatePanel觸發(fā)javascript腳本的方法附代碼
UpdatePanel觸發(fā)javascript腳本的方法附代碼...2007-10-10在線掃描探測(cè)檢查asp站點(diǎn)木馬后門的程序
在線掃描探測(cè)檢查asp站點(diǎn)木馬后門的程序...2007-03-03用asp實(shí)現(xiàn)的數(shù)據(jù)庫(kù)中存取文件的代碼
用asp實(shí)現(xiàn)的數(shù)據(jù)庫(kù)中存取文件的代碼...2007-03-03ASP 獲取文件擴(kuò)展名函數(shù)getFileExt()
利用了asp的instrrev獲取最后.的位置,然后進(jìn)行截取,大家可以自己測(cè)試,其實(shí)編程過(guò)程也是大量的測(cè)試,慢慢就會(huì)發(fā)現(xiàn)更多的好東西。2009-08-08如何從數(shù)據(jù)庫(kù)中隨機(jī)取出10條記錄的方法
如何從數(shù)據(jù)庫(kù)中隨機(jī)取出10條記錄的方法...2007-01-01GetPaing 函數(shù)之a(chǎn)sp采集函數(shù)中用到的獲取分頁(yè)的代碼
GetPaing 函數(shù)之a(chǎn)sp采集函數(shù)中用到的獲取分頁(yè)的代碼...2007-09-09