输入banner图图片脚本导航/分类

asp缓存类

至于缓存的作用,我想我也不用再多说了,它的作用已经很明显,特别是对于信息量非常大或是全数据库页面的网站,他能很好地利用主机的内存资源,加速ASP的执行效率,减轻服务器的负担,而动网在这一方面做得是最突出的,像他现在的dvbbs7.1.0版,更是在缓存的利用上更上一层楼,前后台大多的操作都和缓存有关,而现在动网里用的也就是迷城浪子的缓存类,下面列出动网的三大高手写的ASP缓存类

木鸟写的
代码如下:
'**********************************************
' vbs Cache类

' 属性valid,是否可用,取值前判断
' 属性name,cache名,新建对象后赋值
' 方法add(值,到期时间),设置cache内容
' 属性value,返回cache内容
' 属性blempty,是否未设置值
' 方法makeEmpty,释放内存,测试用
' 方法equal(变量1),判断cache值是否和变量1相同
' 方法expires(time),修改过期时间为time
' 木鸟 2002.12.24
' http://www.gxlsystem.com
'========================
'========================
' Dim Application(2)
' Application(0) Counter 计数器
' Application(1) dateTime 放置时间
' Application(2) Content 缓存内容

Public PREFIX
Public PREFIX_LENGTH

Private Sub Class_Initialize()
PREFIX = "Cached:"
PREFIX_LENGTH = 7
End Sub
Private Sub Class_Terminate
End Sub
' 设置变量
Public Property Let Cache(ByRef Key, ByRef Content)
Dim Item(2)
Item(0) = 0
Item(1) = Now()
IF (IsObject(Content)) Then
Set Item(2) = Content
Else
Item(2) = Content
End IF
Application.Unlock
Application(PREFIX & Key) = Item
Application.Lock
End Property
' 取出变量 计数器++
Public Property Get Cache(ByRef Key)
Dim Item
Item = Application(PREFIX & Key)
IF (IsArray(Item)) Then
IF (IsObject(Item)) Then
Set Cache = Item(2)
Else
Cache = Item(2)
End IF
Application(PREFIX & Key)(0) = Application(PREFIX & Key)(0) + 1
Else
Cache = Empty
End IF
End Property
' 检查缓存对象是否存在
Public Property Get Exists(ByRef Key)
Dim Item
Item = Application(PREFIX & Key)
IF (IsArray(Item)) Then
Exists = True
Else
Exists = False
End IF
End Property
' 得到计数器数值
Public Property Get Counter(ByRef Key)
Dim Item
Item = Application(PREFIX & Key)
IF (IsArray(Item)) Then
Counter = Item(0)
End IF
End Property

' 设置计数器时间
Public Property Let dateTime(ByRef Key, ByRef SetdateTime)
Dim Item
Item = Application(PREFIX & Key)
IF (IsArray(Item)) Then
Item(1) = SetdateTime
End IF
End Property
' 得到计数器时间
Public Property Get dateTime(ByRef Key)
Dim Item
Item = Application(PREFIX & Key)
IF (IsArray(Item)) Then
dateTime = Item(1)
End IF
End Property

' 重置计数器
Public Sub ResetCounter()
Dim Key
Dim Item
Application.Unlock
For Each Key in Application.Contents
IF (Left(Key, PREFIX_LENGTH) = PREFIX) Then
Item = Application(Key)
Item(0) = 0
Application(Key) = Item
End IF
Next
Application.Lock
End Sub
' 删除某以缓存
Public Sub Clear(ByRef Key)
Application.Contents.Remove(PREFIX & Key)
End Sub
' 清空没有使用的缓存
Public Sub ClearUnused()
Dim Key, Keys, KeyLength, KeyIndex
For Each Key in Application.Contents
IF (Left(Key, PREFIX_LENGTH) = PREFIX) Then 
IF (Application(Key)(0) = 0) Then
Keys = Keys & VBNewLine & Key
End IF
End IF
Next
Keys = Split(Keys, VBNewLine)
KeyLength = UBound(Keys)
Application.Unlock 
For KeyIndex = 1 To KeyLength
Application.Contents.Remove(Keys(KeyIndex))
Next
Application.Lock
End Sub
' 清空所有缓存
Public Sub ClearAll()
Dim Key, Keys, KeyLength, KeyIndex
For Each Key in Application.Contents
IF (Left(Key, PREFIX_LENGTH) = PREFIX) Then 
Keys = Keys & VBNewLine & Key
End IF
Next
Keys = Split(Keys, VBNewLine)
KeyLength = UBound(Keys)
Application.Unlock 
For KeyIndex = 1 To KeyLength
Application.Contents.Remove(Keys(KeyIndex))
Next
Application.Lock
End Sub

End Class 
slightboyn 类例子 Set Wyd=New JayCache
Wyd.dateTime("Page")=时 间
If Wyd.Exists("Page") Then
Response.write Wyd.Cache("Page") '输出
Else
Wyd.Cache("Page")=xxx 写入
Responxe.write xxx
End IF
Wyd.Clear("page")'删除缓存