至于緩存的作用,我想我也不用再多說了,它的作用已經(jīng)很明顯,特別是對(duì)于信息量非常大或是全數(shù)據(jù)庫頁面的網(wǎng)站,他能很好地利用主機(jī)的內(nèi)存資源,加速ASP的執(zhí)行效率,減輕服務(wù)器的負(fù)擔(dān),而動(dòng)網(wǎng)在這一方面做得是最突出的,像他現(xiàn)在的dvbbs7.1.0版,更是在緩存的利用上更上一層樓,前后臺(tái)大多的操作都和緩存有關(guān),而現(xiàn)在動(dòng)網(wǎng)里用的也就是迷城浪子的緩存類,下面列出動(dòng)網(wǎng)的三大高手寫的ASP緩存類
木鳥寫的
復(fù)制代碼 代碼如下:
'**********************************************
' vbs Cache類
'
' 屬性valid,是否可用,取值前判斷
' 屬性name,cache名,新建對(duì)象后賦值
' 方法add(值,到期時(shí)間),設(shè)置cache內(nèi)容
' 屬性value,返回cache內(nèi)容
' 屬性blempty,是否未設(shè)置值
' 方法makeEmpty,釋放內(nèi)存,測(cè)試用
' 方法equal(變量1),判斷cache值是否和變量1相同
' 方法expires(time),修改過期時(shí)間為time
' 木鳥 2002.12.24
' http://www.aspsky.net/
'**********************************************
class Cache
private obj 'cache內(nèi)容
private expireTime '過期時(shí)間
private expireTimeName '過期時(shí)間application名
private cacheName 'cache內(nèi)容application名
private path 'uri
private sub class_initialize()
path=request.servervariables("url")
path=left(path,instrRev(path,"/"))
end sub
private sub class_terminate()
end sub
public property get blEmpty
'是否為空
if isempty(obj) then
blEmpty=true
else
blEmpty=false
end if
end property
public property get valid
'是否可用(過期)
if isempty(obj) or not isDate(expireTime) then
valid=false
elseif CDate(expireTime)now then
valid=false
else
valid=true
end if
end property
public property let name(str)
'設(shè)置cache名
cacheName=str path
obj=application(cacheName)
expireTimeName=str "expires" path
expireTime=application(expireTimeName)
end property
public property let expires(tm)
'重設(shè)置過期時(shí)間
expireTime=tm
application.lock
application(expireTimeName)=expireTime
application.unlock
end property
public sub add(var,expire)
'賦值
if isempty(var) or not isDate(expire) then
exit sub
end if
obj=var
expireTime=expire
application.lock
application(cacheName)=obj
application(expireTimeName)=expireTime
application.unlock
end sub
public property get value
'取值
if isempty(obj) or not isDate(expireTime) then
value=null
elseif CDate(expireTime)now then
value=null
else
value=obj
end if
end property
public sub makeEmpty()
'釋放application
application.lock
application(cacheName)=empty
application(expireTimeName)=empty
application.unlock
obj=empty
expireTime=empty
end sub
public function equal(var2)
'比較
if typename(obj)>typename(var2) then
equal=false
elseif typename(obj)="Object" then
if obj is var2 then
equal=true
else
equal=false
end if
elseif typename(obj)="Variant()" then
if join(obj,"^")=join(var2,"^") then
equal=true
else
equal=false
end if
else
if obj=var2 then
equal=true
else
equal=false
end if
end if
end function
end class
木鳥 類例子 vbs Cache類
'
' 屬性valid,是否可用,取值前判斷
' 屬性name,cache名,新建對(duì)象后賦值
' 方法add(值,到期時(shí)間),設(shè)置cache內(nèi)容
' 屬性value,返回cache內(nèi)容
' 屬性blempty,是否未設(shè)置值
' 方法makeEmpty,釋放內(nèi)存,
' 方法DelCahe ,刪除內(nèi)存
' 方法equal(變量1),判斷cache值是否和變量1相同
' 方法expires(time),修改過期時(shí)間為time
' 用法
set myCache=New Cache
myCache.name="BoardJumpList" '定義緩存名
if myCache.valid then?。袛嗍欠窨捎?包括過期,與是否為空值)
response.write myCache.value?。л敵?
else
................
BoardJumpList=xxx
myCache.add BoardJumpList,dateadd("n",60,now)?。懭刖彺?nbsp;xxx.add 內(nèi)容,過期時(shí)間
response.write BoardJumpList '輸出
end if
myCache.makeEmpty() 釋放內(nèi)存
mycache.DelCahe() 刪除緩存
迷城浪子寫的
復(fù)制代碼 代碼如下:
Class Cls_Cache
Rem ==================使用說明====================
Rem = 本類模塊是動(dòng)網(wǎng)先鋒原創(chuàng),作者:迷城浪子。如采用本類模塊,請(qǐng)不要去掉這個(gè)說明。這段注釋不會(huì)影響執(zhí)行的速度。
Rem = 作用:緩存和緩存管理類
Rem = 公有變量:Reloadtime 過期時(shí)間(單位為分鐘)缺省值為14400
Rem = MaxCount 緩存對(duì)象的最大值,超過則自動(dòng)刪除使用次數(shù)少的對(duì)象。缺省值為300
Rem = CacheName 緩存組的總名稱,缺省值為"Dvbbs",如果一個(gè)站點(diǎn)中有超過一個(gè)緩存組,則需要外部改變這個(gè)值。
Rem = 屬性:Name 定義緩存對(duì)象名稱,只寫屬性。
Rem = 屬性:value 讀取和寫入緩存數(shù)據(jù)。
Rem = 函數(shù):ObjIsEmpty()判斷當(dāng)前緩存是否過期。
Rem = 方法:DelCahe(MyCaheName)手工刪除一個(gè)緩存對(duì)象,參數(shù)是緩存對(duì)象的名稱。
Rem ========================
Public Reloadtime,MaxCount,CacheName
Private LocalCacheName,CacheData,DelCount
Private Sub Class_Initialize()
Reloadtime=14400
CacheName="Dvbbs"
End Sub
Private Sub SetCache(SetName,NewValue)
Application.Lock
Application(SetName) = NewValue
Application.unLock
End Sub
Private Sub makeEmpty(SetName)
Application.Lock
Application(SetName) = Empty
Application.unLock
End Sub
Public Property Let Name(ByVal vNewValue)
LocalCacheName=LCase(vNewValue)
End Property
Public Property Let Value(ByVal vNewValue)
If LocalCacheName>"" Then
CacheData=Application(CacheName"_"LocalCacheName)
If IsArray(CacheData) Then
CacheData(0)=vNewValue
CacheData(1)=Now()
Else
ReDim CacheData(2)
CacheData(0)=vNewValue
CacheData(1)=Now()
End If
SetCache CacheName"_"LocalCacheName,CacheData
Else
Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
End If
End Property
Public Property Get Value()
If LocalCacheName>"" Then
CacheData=Application(CacheName"_"LocalCacheName)
If IsArray(CacheData) Then
Value=CacheData(0)
Else
Err.Raise vbObjectError + 1, "DvbbsCacheServer", " The CacheData Is Empty."
End If
Else
Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
End If
End Property
Public Function ObjIsEmpty()
ObjIsEmpty=True
CacheData=Application(CacheName"_"LocalCacheName)
If Not IsArray(CacheData) Then Exit Function
If Not IsDate(CacheData(1)) Then Exit Function
If DateDiff("s",CDate(CacheData(1)),Now()) 60*Reloadtime Then
ObjIsEmpty=False
End If
End Function
Public Sub DelCahe(MyCaheName)
makeEmpty(CacheName"_"MyCaheName)
End Sub
End Class
迷城浪子 類例子
Set WydCache=New Cls_Cache
WydCache.Reloadtime=0.5 '定義過期時(shí)間 (以分鐘為單會(huì))
WydCache.CacheName="pages"?。Фx緩存名
IF WydCache.ObjIsEmpty() Then ''判斷是否可用(包括過期,與是否為空值)
Response.write WydCache.Value
Else
..................
BoardJumpList=xxx
WydCache.Value=BoardJumpList '寫入內(nèi)容
Response.write BoardJumpList
End if
mycache.DelCahe("緩存名") 刪除緩存
slightboy 寫的 '========================
復(fù)制代碼 代碼如下:
'clsCache.asp
'========================
'== begin : 2004-6-26 21:51:47
'== copyright : slightboy (C)1998-2004
'== email : slightboy@msn.com
'========================
'========================
' Dim Application(2)
' Application(0) Counter 計(jì)數(shù)器
' Application(1) dateTime 放置時(shí)間
' Application(2) Content 緩存內(nèi)容
Public PREFIX
Public PREFIX_LENGTH
Private Sub Class_Initialize()
PREFIX = "Cached:"
PREFIX_LENGTH = 7
End Sub
Private Sub Class_Terminate
End Sub
' 設(shè)置變量
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
' 取出變量 計(jì)數(shù)器++
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
' 檢查緩存對(duì)象是否存在
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
' 得到計(jì)數(shù)器數(shù)值
Public Property Get Counter(ByRef Key)
Dim Item
Item = Application(PREFIX Key)
IF (IsArray(Item)) Then
Counter = Item(0)
End IF
End Property
' 設(shè)置計(jì)數(shù)器時(shí)間
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
' 得到計(jì)數(shù)器時(shí)間
Public Property Get dateTime(ByRef Key)
Dim Item
Item = Application(PREFIX Key)
IF (IsArray(Item)) Then
dateTime = Item(1)
End IF
End Property
' 重置計(jì)數(shù)器
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")=時(shí) 間
If Wyd.Exists("Page") Then
Response.write Wyd.Cache("Page") '輸出
Else
Wyd.Cache("Page")=xxx 寫入
Responxe.write xxx
End IF
Wyd.Clear("page")'刪除緩存