分享个asp文件缓存代码,使程序从缓存读数据
作者:何直群 发布时间:2011-03-09 19:47:00
我想从文件读数据的话,要比直接从数据库快一些吧(个人观点),昨天测试了读出6000条新闻,读数据库用了23579毫秒,读文件只用了123毫秒,所以找了个文件缓存的代码,感兴趣的朋友可以研究下,有问题请反馈
使用方法,在需要使用文件缓存的页面加入下列代码,
<!--#include file="FileCatch.asp" -->
<%
Set MyCatch=new CatchFile
MyCatch.Overdue=10 '修改过期时间设置为10分钟
if MyCatch.CatchNow(Rev) then
response.write MyCatch.CatchData
response.end
end if
set MyCatch=nothing
%>
FileCatch.asp
<%
'---- 本文件用于签入原始文件,实现对页面的文件Catch
'---- 1、如果文件请求为POST方式,则取消此功能
'---- 2、文件的请求不能包含系统的识别关键字
'---- 3、作者 何直群 (www.cidianwang.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 Sub
End Class
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=Nothing
End Function
%>
猜你喜欢
- 在sql语句中,如果查找某个文本字段值为空的可以用select * from 表 where 字段=''但是如果
- <% '************************************************
- 下面代码的功能是为了管理任务的,大家项目中有需要用到的,尽管拿去吧。 这个东西用于可以管理任务比如:今天我要做什么,做了没有,做了就打钩。&
- 尝试安装server[justin@xen20-vm04 "]$ rpm -ivh MySQL-server-5.1.51-1.g
- 脚本调试第一步:设置中断(鼠标左键点击)第二步:输入中断条件(可选功能,鼠标右键点击红点)第三步:触发中断(当符合条件是,中断被触发)出现中
- 特点:1.图片预载入,载入后再显示。意图一次呈现,不会让一块一块下载破坏你的页面,绝佳的用户体验,颠覆传统的浏览器呈现图片的处理方式(需要后
- 中文字体设计发展到现在,风格越来越多样化,特别是在广告(美术)字体方面,因为字数少 局限小,优秀的作品层出不穷,比较突出的应用在标志设计唱片
- 参与测试的浏览器:IE6 / IE7 / IE8 / FF3 / OP9.6 / SF3 / Chrome2操作系统:Windows蓝色理想
- 选择正确的数据列类型能大大提高数据库的性能和使数据库具有高扩展性。在选择数据列类型时,请从以下几个方面考虑:存放到数据列中的数据类型。数据值
- 清空服务器缓存asp代码: <%Call RemoveAllCache()Sub RemoveAllCa
- 一.概念简介 脚本:script是使用一种特定的描述性语言,依据一定的格式编写的可执行文件,又称作宏或批处理文件。 二.背景 近来在Wind
- 采用双重循环。把图片进行“.”分割后名字问前面部分,那其余数据库中的 图片路径记录进行对比 采用vb的InStr函数 如果存在的话返回值&g
- 有些使用Z-Blog的用户询问我,怎么实现我的月光博客首页上这种自动图文混排的版式效果,今天我就详细介绍一下在Z-Blog中实现这种图文混排
- 本文帮你六步改善SQL Server安全规划全攻略。一、什么是SQL注入式攻击所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的
- 你是否曾经想在数据库中存储一个日期而没有时间部分,或者想存储一个时间值希望有更高的精度?在SQL Server 2008的介绍中,微软介绍了
- python使用utf8编码,mysql也是utf8编码,是什么问题?后来查了一下,使用一个简单的办法即可:vsql = "ins
- 随着WEB标准在国内的不断普及,结构表现行为分离、模块化、语义化、优雅退化等概念也成为考核一名前端人员对WEB标准理解的重要条目,其中,由于
- 四、XML应用分类 总的说来的XML的应用可分为四类: (1)应用于客户需要与不同的数据源进行交互时。数据可能来自不同的数据库,他们都有各自
- 今天摸了半天摸出来的,虽然这里没啥人玩ASP,不过也是win7的问题,发发当备份问题1.An error occurre
- MySQL数据库由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.本文中列举了一个P2P应用开发实例,实例中使用了MySQL来