熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp從緩存讀數據實例

2013-11-13 10:37:17  來源: .NET編程 
從文件讀數據要比直接從數據庫快測試讀出條新聞讀數據庫用了毫秒讀文件只用了毫秒下例為使用文件緩存的代碼  
使用方法在需要使用文件緩存的頁面加入下列代碼 
<!#include file="FileCatchasp" 
<% 
Set MyCatch=new CatchFile 
MyCatchOverdue=     ’修改過期時間設置為分鐘 
if MyCatchCatchNow(Rev) then 
        responsewrite MyCatchCatchData 
        responseend 
end if 
set MyCatch=nothing 

%> 
FileCatchasp 
復制內容到剪貼板代碼: 
<% 
’ 本文件用於簽入原始文件實現對頁面的文件Catch 
’ 如果文件請求為POST方式則取消此功能 
’ 文件的請求不能包含系統的識別關鍵字 
Class CatchFile 
        Public OverdueMarkCFolderCFile ’定義系統參數 
        Private ScriptNameScriptPathServerHost ’定義服務器/頁面參數變量 
        Public CatchData        ’輸出的數據 
        Private Sub Class_Initialize        ’初始化函數 
                ’獲得服務器及腳本數據 
                ScriptName=RequestServervariables("Script_Name") ’識別出當前腳本的虛擬地址 
                ScriptPath=GetScriptPath(false)        ’識別出腳本的完整GET地址 
                ServerHost=RequestServervariables("Server_Name") ’識別出當前服務器的地址
                ’初始化系統參數 
                Overdue=        ’默認分鐘過期 
                Mark="NoCatch"        ’無Catch請求參數為 NoCatch 
                CFolder=GetCFolder        ’定義默認的Catch文件保存目錄 
                CFile=ServerURLEncode(ScriptPath)&"txt"        ’將腳本路徑轉化為文件路徑 
                CatchData="" 
        end Sub 
        Private Function GetCFolder 
                dim FSOCFolder 
                Set FSO=CreateObject("ScriptingFileSystemObject")        ’設置FSO對象 
                CFolder=ServerMapPath("/")&"/FileCatch/" 
                if not FSOFolderExists(CFolder) then 
                        fsoCreateFolder(CFolder) 
                end if 
                if Month(Now())< then 
                        CFolder=CFolder&"/"&Month(Now()) 
                else 
                        CFolder=CFolder&Month(Now()) 
                end if 
                if Day(Now())< then 
                        CFolder=CFolder&""&Day(Now())
 else 
                        CFolder=CFolder&Day(Now()) 
                end if 
                CFolder=CFolder&"/" 
                if not FSOFolderExists(CFolder) then 
                        fsoCreateFolder(CFolder) 
                end if 
                GetCFolder=CFolder 
                set fso=nothing 
        End Function 
        Private Function bytesBSTR(vIn)        ’轉換編碼的函數 
                dim StrReturnThisCharCodeiNextCharCode 
                strReturn = "" 
                For i =  To LenB(vIn) 
                        ThisCharCode = AscB(MidB(vIni)) 
                        If ThisCharCode < &H Then 
                                strReturn = strReturn & Chr(ThisCharCode) 
                        Else 
                                NextCharCode = AscB(MidB(vIni+)) 
                                strReturn = strReturn & Chr(CLng(ThisCharCode) * &H + CInt(NextCharCode))
                                i = i +  
                        End If 
                Next 
                bytesBSTR = strReturn 
        End Function 
        Public Function CatchNow(Rev)        ’用戶指定開始處理Catch操作 
                if UCase(requestServervariables("Request_Method"))="POST" then 
                ’當是POST方法不可使用文件Catch 
                        Rev="使用POST方法請求頁面不可以使用文件Catch功能" 
                        CatchNow=false 
                else 
                        if requestQuerystring(Mark)<>"" then 
                        ’如果指定參數不為空表示請求不可以使用Catch
 Rev="請求拒絕使用Catch功能" 
                                CatchNow=false 
                        else 
                                CatchNow=GetCatchData(Rev) 
                        end if 
                end if 
        End Function 
        Private Function GetCatchData(Rev)        ’讀取Catch數據 
                Dim FSOIsBuildCatch 
                Set FSO=CreateObject("ScriptingFileSystemObject")        ’設置FSO對象訪問CatchFile 
                If FSOFileExists(CFolder&CFile) Then 
                        Dim FileLastCatch 
                        Set File=FSOGetFile(CFolder&CFile)        ’定義CatchFile文件對象
                        LastCatch=CDate(FileDateLastModified) 
                        if DateDiff("n"LastCatchNow())>Overdue then 
                        ’如果超過了Catch時間 
                                IsBuildCatch=true 
                        else 
                                IsBuildCatch=false 
                        end if 
                        Set File=Nothing 
                else 
                        IsBuildCatch=true 
                End if 
                If IsBuildCatch then 
                        GetCatchData=http://wwwcodecom/html/BuildCatch(Rev)        ’如果需要創建Catch則創建Catch文件同時設置Catch的數據 
                else 
                        GetCatchData=http://wwwcodecom/html/ReadCatch(Rev)        ’如果不需要創建Catch則直接讀取Catch數據 
                End if 
                Set FSO=nothing
 End Function 
        Private Function GetScriptPath(IsGet)        ’創建一個包含所有請求數據的地址 
                dim KeyFir 
                GetScriptPath=ScriptName 
                Fir=true 
                for Each key in RequestQueryString 
                        If Fir then 
                                GetScriptPath=GetScriptPath&"?" 
                                Fir=false 
                        else 
                                GetScriptPath=GetScriptPath&"&" 
                        end if 
                        GetScriptPath=GetScriptPath&ServerURLEncode(Key)&"="&ServerURLEncode(RequestQueryString(Key))
                Next 
                if IsGet then 
                        If Fir then 
                                GetScriptPath=GetScriptPath&"?" 
                                Fir=false 
                        else 
                                GetScriptPath=GetScriptPath&"&" 
                        end if 
                        GetScriptPath=GetScriptPath&ServerURLEncode(Mark)&"=yes" 
                end if 
        End Function 
        ’創建Catch文件 
        Private Function BuildCatch(Rev) 
                Dim HTTPUrlOutCome 
                Set HTTP=CreateObject("MicrosoftXMLHTTP") 
’                On Error Resume Next 
’                responsewrite ServerHost&GetScriptPath(true) 
                HTTPOpen "get""http://"&ServerHost&GetScriptPath(true)False 
                HTTPSend 
                if Errnumber= then 
                        CatchData=http://wwwcodecom/html/bytesBSTR(HTTPresponseBody)
    BuildCatch=True 
                else 
                        Rev="創建發生錯誤"&ErrDescription 
                        BuildCatch=False 
                        Errclear 
                end if 
                Call WriteCatch 
                set HTTP=nothing 
        End Function 
        Private Function ReadCatch(Rev) 
                ReadCatch=IReadCatch(CFolder&CFileCatchDataRev) 
        End Function 
        Private Sub WriteCatch 
                Dim FSOTSO 
                Set FSO=CreateObject("ScriptingFileSystemObject")        ’設置FSO對象訪問CatchFile 
                set TSO=FSOCreateTextFile(CFolder&CFiletrue) 
                TSOWrite(CatchData) 
                Set TSO=Nothing 
                Set FSO=Nothing 
        End Sub 
End Class 
Function IReadCatch(FileDataRev) 
        Dim FSOTSO 
        Set FSO=CreateObject("ScriptingFileSystemObject")        ’設置FSO對象訪問CatchFile 
’        on error resume next 
        set TSO=FSOOpenTextFile(Filefalse) 
        Data=http://wwwcodecom/html/TSOReadAll 
        if Errnumber<> then 
                Rev="讀取發生錯誤"&ErrDescription 
                ReadCatch=False 
                Errclear 
        else 
                IReadCatch=True 
        end if 
        Set TSO=Nothing 
        Set FSO=Nothing 
End Function 
%>

From:http://tw.wingwit.com/Article/program/net/201311/14138.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.