生活随笔
收集整理的這篇文章主要介紹了
简易ASP文件缓存技术
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?注意:系統需要FSO權限、XMLHTTP權限
系統包括兩個文件,其實可以合并為一個。之所以分為兩個是因為部分殺毒軟件會因為里邊含有FSO、XMLHTTP操作而被認為是腳本木馬。
調用時,需要在ASP頁面的最上邊包含主文件,然后在下邊寫下以下代碼
Set MyCatch=new CatchFileMyCatch.Overdue=60*5? ? ? ? '修改過期時間設置為5個小時if MyCatch.CatchNow(Rev) then? ? ? ? response.write MyCatch.CatchData? ? ? ? response.endend ifset MyCatch=nothing 復制代碼
文件一:FileCatch.asp 的代碼
<!--#include file="FileCatch-Inc.asp"--><%'---- 本文件用于簽入原始文件,實現對頁面的文件Catch'---- 1、如果文件請求為POST方式,則取消此功能'---- 2、文件的請求不能包含系統的識別關鍵字'---- 3、作者 何直群 (www.wozhai.com)Class CatchFile? ? ? ? Public Overdue,Mark,CFolder,CFile '定義系統參數? ? ? ? Private ScriptName,ScriptPath,ServerHost '定義服務器/頁面參數變量? ? ? ? Public CatchData? ? ? ? '輸出的數據? ? ? ? Private Sub Class_Initialize? ? ? ? '初始化函數? ? ? ? ? ? ? ? '獲得服務器及腳本數據? ? ? ? ? ? ? ? ScriptName=Request.Servervariables("Script_Name") '識別出當前腳本的虛擬地址? ? ? ? ? ? ? ? ScriptPath=GetScriptPath(false)? ? ? ? '識別出腳本的完整GET地址? ? ? ? ? ? ? ? ServerHost=Request.Servervariables("Server_Name") '識別出當前服務器的地址? ? ? ? ? ? ? ? '初始化系統參數? ? ? ? ? ? ? ? Overdue=30? ? ? ? '默認30分鐘過期? ? ? ? ? ? ? ? Mark="NoCatch"? ? ? ? '無Catch請求參數為 NoCatch? ? ? ? ? ? ? ? CFolder=GetCFolder? ? ? ? '定義默認的Catch文件保存目錄? ? ? ? ? ? ? ? CFile=Server.URLEncode(ScriptPath)&".txt"? ? ? ? '將腳本路徑轉化為文件路徑? ? ? ? ? ? ? ? CatchData=""? ? ? ? end Sub? ? ? ? Private Function GetCFolder? ? ? ? ? ? ? ? dim FSO,CFolder? ? ? ? ? ? ? ? Set FSO=CreateObject("Scripting.FileSystemObject")? ? ? ? '設置FSO對象? ? ? ? ? ? ? ? CFolder=Server.MapPath("/")&"/FileCatch/"? ? ? ? ? ? ? ? if not FSO.FolderExists(CFolder) then? ? ? ? ? ? ? ? ? ? ? ? fso.CreateFolder(CFolder)? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? if Month(Now())<10 then? ? ? ? ? ? ? ? ? ? ? ? CFolder=CFolder&"/0"&Month(Now())? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? CFolder=CFolder&Month(Now())? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? if Day(Now())<10 then? ? ? ? ? ? ? ? ? ? ? ? CFolder=CFolder&"0"&Day(Now())? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? CFolder=CFolder&Day(Now())? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? CFolder=CFolder&"/"? ? ? ? ? ? ? ? if not FSO.FolderExists(CFolder) then? ? ? ? ? ? ? ? ? ? ? ? fso.CreateFolder(CFolder)? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? GetCFolder=CFolder? ? ? ? ? ? ? ? set fso=nothing? ? ? ? End Function? ? ? ? Private Function bytes2BSTR(vIn)? ? ? ? '轉換編碼的函數? ? ? ? ? ? ? ? dim StrReturn,ThisCharCode,i,NextCharCode? ? ? ? ? ? ? ? strReturn = ""? ? ? ? ? ? ? ? For i = 1 To LenB(vIn)? ? ? ? ? ? ? ? ? ? ? ? ThisCharCode = AscB(MidB(vIn,i,1))? ? ? ? ? ? ? ? ? ? ? ? If ThisCharCode < &H80 Then? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? strReturn = strReturn & Chr(ThisCharCode)? ? ? ? ? ? ? ? ? ? ? ? Else? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NextCharCode = AscB(MidB(vIn,i+1,1))? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? i = i + 1? ? ? ? ? ? ? ? ? ? ? ? End If? ? ? ? ? ? ? ? Next? ? ? ? ? ? ? ? bytes2BSTR = strReturn? ? ? ? End Function? ? ? ? Public Function CatchNow(Rev)? ? ? ? '用戶指定開始處理Catch操作? ? ? ? ? ? ? ? if UCase(request.Servervariables("Request_Method"))="POST" then? ? ? ? ? ? ? ? '當是POST方法,不可使用文件Catch? ? ? ? ? ? ? ? ? ? ? ? Rev="使用POST方法請求頁面,不可以使用文件Catch功能"? ? ? ? ? ? ? ? ? ? ? ? CatchNow=false? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? if request.Querystring(Mark)<>"" then? ? ? ? ? ? ? ? ? ? ? ? '如果指定參數不為空,表示請求不可以使用Catch? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Rev="請求拒絕使用Catch功能"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CatchNow=false? ? ? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CatchNow=GetCatchData(Rev)? ? ? ? ? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? end if? ? ? ? End Function? ? ? ? Private Function GetCatchData(Rev)? ? ? ? '讀取Catch數據? ? ? ? ? ? ? ? Dim FSO,IsBuildCatch? ? ? ? ? ? ? ? Set FSO=CreateObject("Scripting.FileSystemObject")? ? ? ? '設置FSO對象,訪問CatchFile? ? ? ? ? ? ? ? If FSO.FileExists(CFolder&CFile) Then? ? ? ? ? ? ? ? ? ? ? ? Dim File,LastCatch? ? ? ? ? ? ? ? ? ? ? ? Set File=FSO.GetFile(CFolder&CFile)? ? ? ? '定義CatchFile文件對象? ? ? ? ? ? ? ? ? ? ? ? LastCatch=CDate(File.DateLastModified)? ? ? ? ? ? ? ? ? ? ? ? if DateDiff("n",LastCatch,Now())>Overdue then? ? ? ? ? ? ? ? ? ? ? ? '如果超過了Catch時間? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? IsBuildCatch=true? ? ? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? IsBuildCatch=false? ? ? ? ? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? ? ? ? ? Set File=Nothing? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? IsBuildCatch=true? ? ? ? ? ? ? ? End if? ? ? ? ? ? ? ? If IsBuildCatch then? ? ? ? ? ? ? ? ? ? ? ? GetCatchData=BuildCatch(Rev)? ? ? ? '如果需要創建Catch,則創建Catch文件,同時設置Catch的數據? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? GetCatchData=ReadCatch(Rev)? ? ? ? '如果不需要創建Catch,則直接讀取Catch數據? ? ? ? ? ? ? ? End if? ? ? ? ? ? ? ? Set FSO=nothing? ? ? ? End Function? ? ? ? Private Function GetScriptPath(IsGet)? ? ? ? '創建一個包含所有請求數據的地址? ? ? ? ? ? ? ? dim Key,Fir? ? ? ? ? ? ? ? GetScriptPath=ScriptName? ? ? ? ? ? ? ? Fir=true? ? ? ? ? ? ? ? for Each key in Request.QueryString? ? ? ? ? ? ? ? ? ? ? ? If Fir then? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? GetScriptPath=GetScriptPath&"?"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Fir=false? ? ? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? GetScriptPath=GetScriptPath&"&"? ? ? ? ? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? ? ? ? ? GetScriptPath=GetScriptPath&Server.URLEncode(Key)&"="&Server.URLEncode(Request.QueryString(Key))? ? ? ? ? ? ? ? Next? ? ? ? ? ? ? ? if IsGet then? ? ? ? ? ? ? ? ? ? ? ? If Fir then? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? GetScriptPath=GetScriptPath&"?"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Fir=false? ? ? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? GetScriptPath=GetScriptPath&"&"? ? ? ? ? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? ? ? ? ? GetScriptPath=GetScriptPath&Server.URLEncode(Mark)&"=yes"? ? ? ? ? ? ? ? end if? ? ? ? End Function? ? ? ? '創建Catch文件? ? ? ? Private Function BuildCatch(Rev)? ? ? ? ? ? ? ? Dim HTTP,Url,OutCome? ? ? ? ? ? ? ? Set HTTP=CreateObject("Microsoft.XMLHTTP")'? ? ? ? ? ? ? ? On Error Resume Next'? ? ? ? ? ? ? ? response.write ServerHost&GetScriptPath(true)? ? ? ? ? ? ? ? HTTP.Open "get","http://"&ServerHost&GetScriptPath(true),False? ? ? ? ? ? ? ? HTTP.Send? ? ? ? ? ? ? ? if Err.number=0 then? ? ? ? ? ? ? ? ? ? ? ? CatchData=bytes2BSTR(HTTP.responseBody)? ? ? ? ? ? ? ? ? ? ? ? BuildCatch=True? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? Rev="創建發生錯誤:"&Err.Description? ? ? ? ? ? ? ? ? ? ? ? BuildCatch=False? ? ? ? ? ? ? ? ? ? ? ? Err.clear? ? ? ? ? ? ? ? end if? ? ? ? ? ? ? ? Call WriteCatch? ? ? ? ? ? ? ? set HTTP=nothing? ? ? ? End Function? ? ? ? Private Function ReadCatch(Rev)? ? ? ? ? ? ? ? ReadCatch=IReadCatch(CFolder&CFile,CatchData,Rev)? ? ? ? End Function? ? ? ? Private Sub WriteCatch? ? ? ? ? ? ? ? Dim FSO,TSO? ? ? ? ? ? ? ? Set FSO=CreateObject("Scripting.FileSystemObject")? ? ? ? '設置FSO對象,訪問CatchFile? ? ? ? ? ? ? ? set TSO=FSO.CreateTextFile(CFolder&CFile,true)? ? ? ? ? ? ? ? TSO.Write(CatchData)? ? ? ? ? ? ? ? Set TSO=Nothing? ? ? ? ? ? ? ? Set FSO=Nothing? ? ? ? End SubEnd Class%> 復制代碼
文件二:FileCatch-Inc.asp
<%Function IReadCatch(File,Data,Rev)? ? ? ? Dim FSO,TSO? ? ? ? Set FSO=CreateObject("Scripting.FileSystemObject")? ? ? ? '設置FSO對象,訪問CatchFile'? ? ? ? on error resume next? ? ? ? set TSO=FSO.OpenTextFile(File,1,false)? ? ? ? Data=TSO.ReadAll? ? ? ? if Err.number<>0 then? ? ? ? ? ? ? ? Rev="讀取發生錯誤:"&Err.Description? ? ? ? ? ? ? ? ReadCatch=False? ? ? ? ? ? ? ? Err.clear? ? ? ? else? ? ? ? ? ? ? ? IReadCatch=True? ? ? ? end if? ? ? ? Set TSO=Nothing? ? ? ? Set FSO=NothingEnd Function%>
復制代碼
總結
以上是生活随笔為你收集整理的简易ASP文件缓存技术的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。