<% '********************************************************************** '@author:jackhuclan,writen 2007-12-03,blog:http://www.cnblogs.com/jackhuclan '@templatePath(模板路径) '@cachePath(缓存路径) '@updateFrequency '缓存更新的频率,-1不更新,0立即更新,数字则每隔几分钟更新 '@ifNeedUpdate 检查是否需要更新 '********************************************************************** Const ForReading =1 Const ForWriting =2 Const ForAppending=8 Const TristateUseDefault=-2 Const TristateTrue=-1 Const TristateFalse=0 Class cache private m_templatePath,m_cachePath,m_updateFrequency Private m_ifNeedUpdate Private fso private templateHandler,cacheHandler Private fileContents Private lastModifyTime Sub class_initialize Set fso=CreateObject("Scripting.FileSystemObject") End Sub Sub class_teminate Set fso=Nothing End Sub PublicPropertyGet templatePath templatePath=m_templatePath End Property PublicPropertyLet templatePath(ByVal value) m_templatePath=server.mappath(value) IfNot fso.FileExists(m_templatePath) Then response.write "不是有效的文件格式,请重新指定要缓存的文件!" response.End Else Set templateHandler=fso.OpenTextFile(m_templatePath,ForReading) fileContents=templateHandler.readall templateHandler.close EndIf End Property PublicPropertyGet cachePath cachePath=m_cachePath End Property PublicPropertyLet cachePath(ByVal value) m_cachePath=server.mappath(value) IfNot (fso.FileExists(m_cachePath)) Then lastModifyTime=Now() Else lastModifyTime=fso.getfile(m_cachePath).DateLastModified EndIf End Property PublicPropertyGet updateFrequency updateFrequency=m_updateFrequency End Property PublicPropertyLet updateFrequency(ByVal value) IfIsEmpty(value) Then m_updateFrequency=-1 ElseIfNotIsNumeric(value) Then m_updateFrequency=-1 ElseIfIsNull(value) Then m_updateFrequency=-1 ElseIfCLng(value)>=0Then m_updateFrequency=CLng(value) Else m_updateFrequency=-1 EndIf End Property publicPropertyGet ifNeedUpdate If m_updateFrequency=-1Then m_ifNeedUpdate=False ElseIf m_updateFrequency=0Then m_ifNeedUpdate=True ElseIf m_updateFrequency>0Then response.write lastModifyTime&"<br>" response.write DateDiff("n",lastModifyTime,Now())&"<br>" response.write m_updateFrequency&"<br>" IfDateDiff("n",lastModifyTime,Now())>m_updateFrequency Then m_ifNeedUpdate=True Else m_ifNeedUpdate=False EndIf Else m_ifNeedUpdate=False EndIf ifNeedUpdate=m_ifNeedUpdate End Property PublicFunction AssignValue(ByVal tag,ByVal value) fileContents=Replace(fileContents,tag,value) AssignValue=fileContents End Function PublicSub UpdateCache() If ifNeedUpdate Then Set cacheHandler=fso.OpenTextFile(m_cachePath,ForWriting,true) If Err Then Err.clear cacheHandler.close response.write "更新文件失败!" response.End EndIf cacheHandler.write fileContents cacheHandler.close EndIf End Sub PublicSub display() Dim f Set f=fso.OpenTextFile(m_cachePath,ForReading) response.write f.readall() f.close End Sub End class %>
该类在web层中的使用。
Dim news_cache Dim path1:path1="/web/template/news/index.html" Dim path2:path2="/web/news/index.html" 'response.write ShowFileAccessInfo(server.mappath(path1)) Set news_cache=new cache with news_cache .templatePath=path1 .cachePath=path2 .updateFrequency=-1 .assignValue "{$title}","新标题" .assignValue "{$body}","新正文" .updatecache .display Endwith