asp定時產生靜態HTML的程式碼,對於緩解伺服器壓力有很大幫主,需要的朋友可以參考下。
複製程式碼程式碼如下:<%
'判斷是否要產生新的HTML
if Application(cache_asptohtml_date)= then
Application.Lock
Application(cache_asptohtml_date)=now()
Application.Unlock
Call aspTohtml
Response.Redirect(index.html)
end if
DateDateDiff (s, Application(cache_asptohtml_date),Now)> 100 then '比較上次更新時間與目前時間相差多少秒
Application.Lock
Application(cache_asptohtml_date)=now()
Application.UnLock
Call aspTohtml
Response.Redirect(index.html)
Else
Response.Redirect(index.html)
End if
'取得目前目錄!
function getpath
if Request.ServerVariables(SERVER_PORT)<>80 then
UserUrl = http://&Request.ServerVariables(
SERVER_NAME)& : & Request.ServerVariables(SERVER_PORT)& Request.ServerVariables(URL)
else
UserUrl = http://&Request.ServerVariables(SERVER_NAME)& Request.ServerVariabled
getpath=left(UserUrl,InstrRev(UserUrl,/))
end function
sub aspTohtml
'-------------------------------- --------------------------
'使用XMLHTTP產生靜態首頁的程式碼
'Curl 為你的首頁位址,確保你的空間支援FSO
'- -------------------------------------------------- --------
dim read,Curl,content
Curl=getpath&home.asp
read=getHTTPPage(Curl)
if read<> then
content=read
Set Fso = Server.CreateObject(Scripting.FileSystemObject)
Filen
=Server.MapPath(index.html)
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close.Close.Close
.
End sub
Function getHTTPPage(url)
dim http
set http=Server.createobject(Microsoft.XMLHTTP)
Http.open GET,url,false
Http.send()
if
Http.readystate<>4 then
exit function
end if
getHTTPPage
=bytesToBSTR(Http.responseBody, http23120)
err.number<>0 then err.Clear
End Function
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
. Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
%>
ASP定時產生靜態頁(HTML)2
複製程式碼如下:
<%
set fs=server.createobject(scripting.filesystemobject)
file=server.mappath(time. )
set txt=fs.opentextfile(file,1,true)
if not txt.atendofstream then
times=txt.ReadLine
Else
response.write <br /><!--有沒有發現time.txt 開始產生!-->
HtmlPage = time.txt '//產生的HTML檔名
Template = NOW()
Set FSO = Server.CreateObject (Scripting.FileSystemObject)
Set FileOut = FSO.CreateTextFile(Server.MapPath (HtmlPage))
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
end If
If datediff(s,times,NOW()) > 3600 Then '//上次更新到現在的時間大於3600秒則更新
response.write <br />< !--時間過了開始更新-->
code =這裡是需要產生的html代碼'//如何得到程式碼的方式有很多
'//用FSO產生HTML頁面
HtmlPage = index.html '//產生的HTML檔名
Template = code
Set FSO = Server.CreateObject (Scripting. FileSystemObject)
Set FileOut = FSO.CreateTextFile(Server.MapPath (HtmlPage))
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
'//用FSO產生time.txt檔案
HtmlPage = time.txt '//產生的HTML檔案名稱
Template = NOW()
Set FSO = Server.CreateObject (Scripting.FileSystemObject)
Set FileOut = FSO.CreateTextFile(Server .MapPath (HtmlPage))
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
Else
response.write <br /><!-- 已經過去&datediff(s,times,NOW())&秒!-->
End If
%>