现在的位置: 首页 > 综合 > 正文

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

2012年10月26日 ⁄ 综合 ⁄ 共 4112字 ⁄ 字号 评论关闭
这节讲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

没什么注释,大家看要耐心啊,哈哈!恳请批评和指正。

抱歉!评论已关闭.