至於快取的作用,我想我也不用再多說了,它的作用已經很明顯,特別是對於資訊量非常大或是全資料庫頁面的網站,他能很好地利用主機的記憶體資源,加速ASP的執行效率,減輕服務器的.負擔,而動網在這一方面做得是最突出的,像他現在的dvbbs7.1.0版,更是在快取的利用上更上一層樓,前後臺大多的操作都和快取有關,而現在動網裡用的也就是迷城浪子的快取類,下面列出動網的三大高手寫的ASP快取類
木鳥寫的
複製程式碼 程式碼如下:
'***
'vbsCache類
'
'屬性valid,是否可用,取值前判斷
'屬性name,cache名,新建物件後賦值
'方法add(值,到期時間),設定cache內容
'屬性value,返回cache內容
'屬性blempty,是否未設定值
'方法makeEmpty,釋放記憶體,測試用
'方法equal(變數1),判斷cache值是否和變數1相同
'方法expires(time),修改過期時間為time
'木鳥2002.12.24
'
'***
classCache
privateobj'cache內容
privateexpireTime'過期時間
privateexpireTimeName'過期時間application名
privatecacheName'cache內容application名
privatepath'uri
privatesubclass_initialize()
path=ervariables("url")
path=left(path,instrRev(path,"/"))
endsub
privatesubclass_terminate()
endsub
publicpropertygetblEmpty
'是否為空
ifisempty(obj)then
blEmpty=true
else
blEmpty=false
endif
endproperty
publicpropertygetvalid
'是否可用(過期)
ifisempty(obj)ornotisDate(expireTime)then
valid=false
elseifCDate(expireTime)<nowthen
valid=false
else
valid=true
endif
endproperty
publicpropertyletname(str)
'設定cache名
cacheName=str&path
obj=application(cacheName)
expireTimeName=str&"expires"&path
expireTime=application(expireTimeName)
endproperty
publicpropertyletexpires(tm)
'重設定過期時間
expireTime=tm
application(expireTimeName)=expireTime
ck
endproperty
publicsubadd(var,expire)
'賦值
ifisempty(var)ornotisDate(expire)then
exitsub
endif
obj=var
expireTime=expire
application(cacheName)=obj
application(expireTimeName)=expireTime
ck
endsub
publicpropertygetvalue
'取值
ifisempty(obj)ornotisDate(expireTime)then
value=null
elseifCDate(expireTime)<nowthen
value=null
else
value=obj
endif
endproperty
publicsubmakeEmpty()
'釋放application
application(cacheName)=empty
application(expireTimeName)=empty
ck
obj=empty
expireTime=empty
endsub
publicfunctionequal(var2)
'比較
iftypename(obj)<>typename(var2)then
equal=false
elseiftypename(obj)="Object"then
ifobjisvar2then
equal=true
else
equal=false
endif
elseiftypename(obj)="Variant()"then
ifjoin(obj,"^")=join(var2,"^")then
equal=true
else
equal=false
endif
else
ifobj=var2then
equal=true
else
equal=false
endif
endif
endfunction
endclass
木鳥類例子vbsCache類
'
'屬性valid,是否可用,取值前判斷
'屬性name,cache名,新建物件後賦值
'方法add(值,到期時間),設定cache內容
'屬性value,返回cache內容
'屬性blempty,是否未設定值
'方法makeEmpty,釋放記憶體,
'方法DelCahe,刪除記憶體
'方法equal(變數1),判斷cache值是否和變數1相同
'方法expires(time),修改過期時間為time
'用法
setmyCache=NewCache
="BoardJumpList"'定義快取名
dthen'判斷是否可用(包括過期,與是否為空值)
e'輸出
else
................
BoardJumpList=xxx
oardJumpList,dateadd("n",60,now)'寫入快取內容,過期時間
eBoardJumpList'輸出
endif
Empty()釋放記憶體
ahe()刪除快取
迷城浪子寫的
複製程式碼 程式碼如下:
ClassCls_Cache
Rem==================使用說明====================
Rem=本類模組是動網先鋒原創,作者:迷城浪子。如採用本類模組,請不要去掉這個說明。這段註釋不會影響執行的速度。
Rem=作用:快取和快取管理類
Rem=公有變數:Reloadtime過期時間(單位為分鐘)預設值為14400
Rem=MaxCount快取物件的最大值,超過則自動刪除使用次數少的物件。預設值為300
Rem=CacheName快取組的總名稱,預設值為"Dvbbs",如果一個站點中有超過一個快取組,則需要外部改變這個值。
Rem=屬性:Name定義快取物件名稱,只寫屬性。
Rem=屬性:value讀取和寫入快取資料。
Rem=函式:ObjIsEmpty()判斷當前快取是否過期。
Rem=方法:DelCahe(MyCaheName)手工刪除一個快取物件,引數是快取物件的名稱。
Rem========================
PublicReloadtime,MaxCount,CacheName
PrivateLocalCacheName,CacheData,DelCount
PrivateSubClass_Initialize()
Reloadtime=14400
CacheName="Dvbbs"
EndSub
PrivateSubSetCache(SetName,NewValue)
Application(SetName)=NewValue
ck
EndSub
PrivateSubmakeEmpty(SetName)
Application(SetName)=Empty
ck
EndSub
PublicPropertyLetName(ByValvNewValue)
LocalCacheName=LCase(vNewValue)
EndProperty
PublicPropertyLetValue(ByValvNewValue)
IfLocalCacheName<>""Then
CacheData=Application(CacheName&"_"&LocalCacheName)
IfIsArray(CacheData)Then
CacheData(0)=vNewValue
CacheData(1)=Now()
Else
ReDimCacheData(2)
CacheData(0)=vNewValue
CacheData(1)=Now()
EndIf
SetCacheCacheName&"_"&LocalCacheName,CacheData
Else
evbObjectError+1,"DvbbsCacheServer","pleasechangetheCacheName."
EndIf
EndProperty
PublicPropertyGetValue()
IfLocalCacheName<>""Then
CacheData=Application(CacheName&"_"&LocalCacheName)
IfIsArray(CacheData)Then
Value=CacheData(0)
Else
evbObjectError+1,"DvbbsCacheServer","TheCacheDataIsEmpty."
EndIf
Else
evbObjectError+1,"DvbbsCacheServer","pleasechangetheCacheName."
EndIf
EndProperty
PublicFunctionObjIsEmpty()
ObjIsEmpty=True
CacheData=Application(CacheName&"_"&LocalCacheName)
IfNotIsArray(CacheData)ThenExitFunction
IfNotIsDate(CacheData(1))ThenExitFunction
IfDateDiff("s",CDate(CacheData(1)),Now())<60*ReloadtimeThen
ObjIsEmpty=False
EndIf
EndFunction
PublicSubDelCahe(MyCaheName)
makeEmpty(CacheName&"_"&MyCaheName)
EndSub
EndClass
迷城浪子類例子
SetWydCache=NewCls_Cache
adtime=0.5'定義過期時間(以分鐘為單會)
eName="pages"'定義快取名
sEmpty()Then''判斷是否可用(包括過期,與是否為空值)
e
Else
..................
BoardJumpList=xxx
e=BoardJumpList'寫入內容
eBoardJumpList
Endif
ahe("快取名")刪除快取
slightboy寫的'========================
複製程式碼 程式碼如下:
'
'========================
'==begin:2004-6-2621:51:47
'==copyright:slightboy(C)1998-2004
'
'========================
'========================
'DimApplication(2)
'Application(0)Counter計數器
'Application(1)dateTime放置時間
'Application(2)Content快取內容
PublicPREFIX
PublicPREFIX_LENGTH
PrivateSubClass_Initialize()
PREFIX="Cached:"
PREFIX_LENGTH=7
EndSub
PrivateSubClass_Terminate
EndSub
'設定變數
PublicPropertyLetCache(ByRefKey,ByRefContent)
DimItem(2)
Item(0)=0
Item(1)=Now()
IF(IsObject(Content))Then
SetItem(2)=Content
Else
Item(2)=Content
EndIF
ck
Application(PREFIX&Key)=Item
EndProperty
'取出變數計數器++
PublicPropertyGetCache(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
IF(IsObject(Item))Then
SetCache=Item(2)
Else
Cache=Item(2)
EndIF
Application(PREFIX&Key)(0)=Application(PREFIX&Key)(0)+1
Else
Cache=Empty
EndIF
EndProperty
'檢查快取物件是否存在
PublicPropertyGetExists(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
Exists=True
Else
Exists=False
EndIF
EndProperty
'得到計數器數值
PublicPropertyGetCounter(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
Counter=Item(0)
EndIF
EndProperty
'設定計數器時間
PublicPropertyLetdateTime(ByRefKey,ByRefSetdateTime)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
Item(1)=SetdateTime
EndIF
EndProperty
'得到計數器時間
PublicPropertyGetdateTime(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
dateTime=Item(1)
EndIF
EndProperty
'重置計數器
PublicSubResetCounter()
DimKey
DimItem
ck
ents
IF(Left(Key,PREFIX_LENGTH)=PREFIX)Then
Item=Application(Key)
Item(0)=0
Application(Key)=Item
EndIF
Next
EndSub
'刪除某以快取
PublicSubClear(ByRefKey)
ve(PREFIX&Key)
EndSub
'清空沒有使用的快取
PublicSubClearUnused()
DimKey,Keys,KeyLength,KeyIndex
ents
IF(Left(Key,PREFIX_LENGTH)=PREFIX)Then
IF(Application(Key)(0)=0)Then
Keys=Keys&VBNewLine&Key
EndIF
EndIF
Next
Keys=Split(Keys,VBNewLine)
KeyLength=UBound(Keys)
ck
ForKeyIndex=1ToKeyLength
ve(Keys(KeyIndex))
Next
EndSub
'清空所有快取
PublicSubClearAll()
DimKey,Keys,KeyLength,KeyIndex
ents
IF(Left(Key,PREFIX_LENGTH)=PREFIX)Then
Keys=Keys&VBNewLine&Key
EndIF
Next
Keys=Split(Keys,VBNewLine)
KeyLength=UBound(Keys)
ck
ForKeyIndex=1ToKeyLength
ve(Keys(KeyIndex))
Next
EndSub
EndClass
slightboyn類例子SetWyd=NewJayCache
Time("Page")=時間
ts("Page")Then
e("Page")'輸出
Else
e("Page")=xxx寫入
exxx
EndIF
r("page")'刪除快取