简易ASP文件缓存技术

 注意:系统需要FSO权限、XMLHTTP权限

系统包括两个文件,其实可以合并为一个。之所以分为两个是因为部分杀毒软件会因为里边含有FSO、XMLHTTP操作而被认为是脚本木马。

调用时,需要在ASP页面的最上边包含主文件,然后在下边写下以下代码

  1. Set MyCatch=new CatchFile
  2. MyCatch.Overdue=60*5        '修改过期时间设置为5个小时
  3. if MyCatch.CatchNow(Rev) then
  4.         response.write MyCatch.CatchData
  5.         response.end
  6. end if
  7. set MyCatch=nothing
复制代码

文件一:FileCatch.asp 的代码

  1. <!--#include file="FileCatch-Inc.asp"-->
  2. <%
  3. '---- 本文件用于签入原始文件,实现对页面的文件Catch
  4. '---- 1、如果文件请求为POST方式,则取消此功能
  5. '---- 2、文件的请求不能包含系统的识别关键字
  6. '---- 3、作者 何直群 (www.wozhai.com)
  7. Class CatchFile
  8.         Public Overdue,Mark,CFolder,CFile '定义系统参数
  9.         Private ScriptName,ScriptPath,ServerHost '定义服务器/页面参数变量
  10.         Public CatchData        '输出的数据
  11.         Private Sub Class_Initialize        '初始化函数
  12.                 '获得服务器及脚本数据
  13.                 ScriptName=Request.Servervariables("Script_Name") '识别出当前脚本的虚拟地址
  14.                 ScriptPath=GetScriptPath(false)        '识别出脚本的完整GET地址
  15.                 ServerHost=Request.Servervariables("Server_Name") '识别出当前服务器的地址
  16.                 '初始化系统参数
  17.                 Overdue=30        '默认30分钟过期
  18.                 Mark="NoCatch"        '无Catch请求参数为 NoCatch
  19.                 CFolder=GetCFolder        '定义默认的Catch文件保存目录
  20.                 CFile=Server.URLEncode(ScriptPath)&".txt"        '将脚本路径转化为文件路径
  21.                 CatchData=""
  22.         end Sub
  23.         Private Function GetCFolder
  24.                 dim FSO,CFolder
  25.                 Set FSO=CreateObject("Scripting.FileSystemObject")        '设置FSO对象
  26.                 CFolder=Server.MapPath("/")&"/FileCatch/"
  27.                 if not FSO.FolderExists(CFolder) then
  28.                         fso.CreateFolder(CFolder)
  29.                 end if
  30.                 if Month(Now())<10 then
  31.                         CFolder=CFolder&"/0"&Month(Now())
  32.                 else
  33.                         CFolder=CFolder&Month(Now())
  34.                 end if
  35.                 if Day(Now())<10 then
  36.                         CFolder=CFolder&"0"&Day(Now())
  37.                 else
  38.                         CFolder=CFolder&Day(Now())
  39.                 end if
  40.                 CFolder=CFolder&"/"
  41.                 if not FSO.FolderExists(CFolder) then
  42.                         fso.CreateFolder(CFolder)
  43.                 end if
  44.                 GetCFolder=CFolder
  45.                 set fso=nothing
  46.         End Function
  47.         Private Function bytes2BSTR(vIn)        '转换编码的函数
  48.                 dim StrReturn,ThisCharCode,i,NextCharCode
  49.                 strReturn = ""
  50.                 For i = 1 To LenB(vIn)
  51.                         ThisCharCode = AscB(MidB(vIn,i,1))
  52.                         If ThisCharCode < &H80 Then
  53.                                 strReturn = strReturn & Chr(ThisCharCode)
  54.                         Else
  55.                                 NextCharCode = AscB(MidB(vIn,i+1,1))
  56.                                 strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
  57.                                 i = i + 1
  58.                         End If
  59.                 Next
  60.                 bytes2BSTR = strReturn
  61.         End Function
  62.         Public Function CatchNow(Rev)        '用户指定开始处理Catch操作
  63.                 if UCase(request.Servervariables("Request_Method"))="POST" then
  64.                 '当是POST方法,不可使用文件Catch
  65.                         Rev="使用POST方法请求页面,不可以使用文件Catch功能"
  66.                         CatchNow=false
  67.                 else
  68.                         if request.Querystring(Mark)<>"" then
  69.                         '如果指定参数不为空,表示请求不可以使用Catch
  70.                                 Rev="请求拒绝使用Catch功能"
  71.                                 CatchNow=false
  72.                         else
  73.                                 CatchNow=GetCatchData(Rev)
  74.                         end if
  75.                 end if
  76.         End Function
  77.         Private Function GetCatchData(Rev)        '读取Catch数据
  78.                 Dim FSO,IsBuildCatch
  79.                 Set FSO=CreateObject("Scripting.FileSystemObject")        '设置FSO对象,访问CatchFile
  80.                 If FSO.FileExists(CFolder&CFile) Then
  81.                         Dim File,LastCatch
  82.                         Set File=FSO.GetFile(CFolder&CFile)        '定义CatchFile文件对象
  83.                         LastCatch=CDate(File.DateLastModified)
  84.                         if DateDiff("n",LastCatch,Now())>Overdue then
  85.                         '如果超过了Catch时间
  86.                                 IsBuildCatch=true
  87.                         else
  88.                                 IsBuildCatch=false
  89.                         end if
  90.                         Set File=Nothing
  91.                 else
  92.                         IsBuildCatch=true
  93.                 End if
  94.                 If IsBuildCatch then
  95.                         GetCatchData=BuildCatch(Rev)        '如果需要创建Catch,则创建Catch文件,同时设置Catch的数据
  96.                 else
  97.                         GetCatchData=ReadCatch(Rev)        '如果不需要创建Catch,则直接读取Catch数据
  98.                 End if
  99.                 Set FSO=nothing
  100.         End Function
  101.         Private Function GetScriptPath(IsGet)        '创建一个包含所有请求数据的地址
  102.                 dim Key,Fir
  103.                 GetScriptPath=ScriptName
  104.                 Fir=true
  105.                 for Each key in Request.QueryString
  106.                         If Fir then
  107.                                 GetScriptPath=GetScriptPath&"?"
  108.                                 Fir=false
  109.                         else
  110.                                 GetScriptPath=GetScriptPath&"&"
  111.                         end if
  112.                         GetScriptPath=GetScriptPath&Server.URLEncode(Key)&"="&Server.URLEncode(Request.QueryString(Key))
  113.                 Next
  114.                 if IsGet then
  115.                         If Fir then
  116.                                 GetScriptPath=GetScriptPath&"?"
  117.                                 Fir=false
  118.                         else
  119.                                 GetScriptPath=GetScriptPath&"&"
  120.                         end if
  121.                         GetScriptPath=GetScriptPath&Server.URLEncode(Mark)&"=yes"
  122.                 end if
  123.         End Function
  124.         '创建Catch文件
  125.         Private Function BuildCatch(Rev)
  126.                 Dim HTTP,Url,OutCome
  127.                 Set HTTP=CreateObject("Microsoft.XMLHTTP")
  128. '                On Error Resume Next
  129. '                response.write ServerHost&GetScriptPath(true)
  130.                 HTTP.Open "get","http://"&ServerHost&GetScriptPath(true),False
  131.                 HTTP.Send
  132.                 if Err.number=0 then
  133.                         CatchData=bytes2BSTR(HTTP.responseBody)
  134.                         BuildCatch=True
  135.                 else
  136.                         Rev="创建发生错误:"&Err.Description
  137.                         BuildCatch=False
  138.                         Err.clear
  139.                 end if
  140.                 Call WriteCatch
  141.                 set HTTP=nothing
  142.         End Function
  143.         Private Function ReadCatch(Rev)
  144.                 ReadCatch=IReadCatch(CFolder&CFile,CatchData,Rev)
  145.         End Function
  146.         Private Sub WriteCatch
  147.                 Dim FSO,TSO
  148.                 Set FSO=CreateObject("Scripting.FileSystemObject")        '设置FSO对象,访问CatchFile
  149.                 set TSO=FSO.CreateTextFile(CFolder&CFile,true)
  150.                 TSO.Write(CatchData)
  151.                 Set TSO=Nothing
  152.                 Set FSO=Nothing
  153.         End Sub
  154. End Class
  155. %>
复制代码

文件二:FileCatch-Inc.asp

  1. <%
  2. Function IReadCatch(File,Data,Rev)
  3.         Dim FSO,TSO
  4.         Set FSO=CreateObject("Scripting.FileSystemObject")        '设置FSO对象,访问CatchFile
  5. '        on error resume next
  6.         set TSO=FSO.OpenTextFile(File,1,false)
  7.         Data=TSO.ReadAll
  8.         if Err.number<>0 then
  9.                 Rev="读取发生错误:"&Err.Description
  10.                 ReadCatch=False
  11.                 Err.clear
  12.         else
  13.                 IReadCatch=True
  14.         end if
  15.         Set TSO=Nothing
  16.         Set FSO=Nothing
  17. End Function
  18. %>
复制代码
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值