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

这节讲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


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

转载于:https://www.cnblogs.com/jackhuclan/archive/2008/01/10/1033341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值