您的位置:首页 > 编程语言 > ASP

关于asp多层体系架构的思考(2)

2008-01-10 11:58 441 查看
这节讲common文件夹中缓存类(cache.class.asp)的实现。

1)实现了html缓存。

2)html模板的定时更新。

3)实现php的smarty少部分模板机制。

4)更多功能完善中......

<%

'**********************************************************************

'@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

Public Property Get templatePath

templatePath=m_templatePath

End Property

Public Property Let templatePath(ByVal value)

m_templatePath=server.mappath(value)

If Not fso.FileExists(m_templatePath) Then

response.write "不是有效的文件格式,请重新指定要缓存的文件!"

response.End

Else

Set templateHandler=fso.OpenTextFile(m_templatePath,ForReading)

fileContents=templateHandler.readall

templateHandler.close

End If

End Property

Public Property Get cachePath

cachePath=m_cachePath

End Property

Public Property Let cachePath(ByVal value)

m_cachePath=server.mappath(value)

If Not (fso.FileExists(m_cachePath)) Then

lastModifyTime=Now()

Else

lastModifyTime=fso.getfile(m_cachePath).DateLastModified

End If

End Property

Public Property Get updateFrequency

updateFrequency=m_updateFrequency

End Property

Public Property Let updateFrequency(ByVal value)

If IsEmpty(value) Then

m_updateFrequency=-1

ElseIf Not IsNumeric(value) Then

m_updateFrequency=-1

ElseIf IsNull(value) Then

m_updateFrequency=-1

ElseIf CLng(value)>=0 Then

m_updateFrequency=CLng(value)

Else

m_updateFrequency=-1

End If

End Property

public Property Get ifNeedUpdate

If m_updateFrequency=-1 Then

m_ifNeedUpdate=False

ElseIf m_updateFrequency=0 Then

m_ifNeedUpdate=True

ElseIf m_updateFrequency>0 Then

response.write lastModifyTime&"<br>"

response.write DateDiff("n",lastModifyTime,Now())&"<br>"

response.write m_updateFrequency&"<br>"

If DateDiff("n",lastModifyTime,Now())>m_updateFrequency Then

m_ifNeedUpdate=True

Else

m_ifNeedUpdate=False

End If

Else

m_ifNeedUpdate=False

End If

ifNeedUpdate=m_ifNeedUpdate

End Property

Public Function AssignValue(ByVal tag,ByVal value)

fileContents=Replace(fileContents,tag,value)

AssignValue=fileContents

End Function

Public Sub UpdateCache()

If ifNeedUpdate Then

Set cacheHandler=fso.OpenTextFile(m_cachePath,ForWriting,true)

If Err Then

Err.clear

cacheHandler.close

response.write "更新文件失败!"

response.End

End If

cacheHandler.write fileContents

cacheHandler.close

End If

End Sub

Public Sub 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

End with

没什么注释,大家看要耐心啊,哈哈!恳请批评和指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: