asp緩存類代碼
至于緩存的作用,我想我也不用再多說了,它的作用已經(jīng)很明顯,特別是對于信息量非常大或是全數(shù)據(jù)庫頁面的網(wǎng)站,他能很好地利用主機的內(nèi)存資源,加速ASP的執(zhí)行效率,減輕服務器的負擔,而動網(wǎng)在這一方面做得是最突出的,像他現(xiàn)在的dvbbs7.1.0版,更是在緩存的利用上更上一層樓,前后臺大多的操作都和緩存有關,而現(xiàn)在動網(wǎng)里用的也就是迷城浪子的緩存類,下面列出動網(wǎng)的.三大高手寫的ASP緩存類
木鳥寫的
復制代碼 代碼如下:
'***
。bsCache類
。
'屬性valid,是否可用,取值前判斷
。傩詎ame,cache名,新建對象后賦值
。Х椒╝dd(值,到期時間),設置cache內(nèi)容
'屬性value,返回cache內(nèi)容
。傩詁lempty,是否未設置值
。Х椒╩akeEmpty,釋放內(nèi)存,測試用
。Х椒╡qual(變量1),判斷cache值是否和變量1相同
'方法expires(time),修改過期時間為time
。绝B2002.12.24
。ttp://www.aspsky.net/
。***
classCache
privateobj'cache內(nèi)容
privateexpireTime'過期時間
privateexpireTimeName'過期時間application名
privatecacheName'cache內(nèi)容application名
privatepath'uri
privatesubclass_initialize()
path=request.servervariables("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)
。гO置cache名
cacheName=str&path
obj=application(cacheName)
expireTimeName=str&"expires"&path
expireTime=application(expireTimeName)
endproperty
publicpropertyletexpires(tm)
'重設置過期時間
expireTime=tm
application.lock
application(expireTimeName)=expireTime
application.unlock
endproperty
publicsubadd(var,expire)
。зx值
ifisempty(var)ornotisDate(expire)then
exitsub
endif
obj=var
expireTime=expire
application.lock
application(cacheName)=obj
application(expireTimeName)=expireTime
application.unlock
endsub
publicpropertygetvalue
'取值
ifisempty(obj)ornotisDate(expireTime)then
value=null
elseifCDate(expireTime)<nowthen
value=null
else
value=obj
endif
endproperty
publicsubmakeEmpty()
。п尫臿pplication
application.lock
application(cacheName)=empty
application(expireTimeName)=empty
application.unlock
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類
。
。傩詖alid,是否可用,取值前判斷
。傩詎ame,cache名,新建對象后賦值
'方法add(值,到期時間),設置cache內(nèi)容
。傩詖alue,返回cache內(nèi)容
。傩詁lempty,是否未設置值
。Х椒╩akeEmpty,釋放內(nèi)存,
'方法DelCahe,刪除內(nèi)存
。Х椒╡qual(變量1),判斷cache值是否和變量1相同
'方法expires(time),修改過期時間為time
'用法
setmyCache=NewCache
myCache.name="BoardJumpList"'定義緩存名
ifmyCache.validthen'判斷是否可用(包括過期,與是否為空值)
response.writemyCache.value'輸出
else
................
BoardJumpList=xxx
myCache.addBoardJumpList,dateadd("n",60,now)'寫入緩存xxx.add內(nèi)容,過期時間
response.writeBoardJumpList'輸出
endif
myCache.makeEmpty()釋放內(nèi)存
mycache.DelCahe()刪除緩存
迷城浪子寫的
復制代碼 代碼如下:
ClassCls_Cache
Rem==================使用說明====================
Rem=本類模塊是動網(wǎng)先鋒原創(chuàng),作者:迷城浪子。如采用本類模塊,請不要去掉這個說明。這段注釋不會影響執(zhí)行的速度。
Rem=作用:緩存和緩存管理類
Rem=公有變量:Reloadtime過期時間(單位為分鐘)缺省值為14400
Rem=MaxCount緩存對象的最大值,超過則自動刪除使用次數(shù)少的對象。缺省值為300
Rem=CacheName緩存組的總名稱,缺省值為"Dvbbs",如果一個站點中有超過一個緩存組,則需要外部改變這個值。
Rem=屬性:Name定義緩存對象名稱,只寫屬性。
Rem=屬性:value讀取和寫入緩存數(shù)據(jù)。
Rem=函數(shù):ObjIsEmpty()判斷當前緩存是否過期。
Rem=方法:DelCahe(MyCaheName)手工刪除一個緩存對象,參數(shù)是緩存對象的名稱。
Rem========================
PublicReloadtime,MaxCount,CacheName
PrivateLocalCacheName,CacheData,DelCount
PrivateSubClass_Initialize()
Reloadtime=14400
CacheName="Dvbbs"
EndSub
PrivateSubSetCache(SetName,NewValue)
Application.Lock
Application(SetName)=NewValue
Application.unLock
EndSub
PrivateSubmakeEmpty(SetName)
Application.Lock
Application(SetName)=Empty
Application.unLock
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
Err.RaisevbObjectError+1,"DvbbsCacheServer","pleasechangetheCacheName."
EndIf
EndProperty
PublicPropertyGetValue()
IfLocalCacheName<>""Then
CacheData=Application(CacheName&"_"&LocalCacheName)
IfIsArray(CacheData)Then
Value=CacheData(0)
Else
Err.RaisevbObjectError+1,"DvbbsCacheServer","TheCacheDataIsEmpty."
EndIf
Else
Err.RaisevbObjectError+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
WydCache.Reloadtime=0.5'定義過期時間(以分鐘為單會)
WydCache.CacheName="pages"'定義緩存名
IFWydCache.ObjIsEmpty()Then''判斷是否可用(包括過期,與是否為空值)
Response.writeWydCache.Value
Else
..................
BoardJumpList=xxx
WydCache.Value=BoardJumpList'寫入內(nèi)容
Response.writeBoardJumpList
Endif
mycache.DelCahe("緩存名")刪除緩存
slightboy寫的'========================
復制代碼 代碼如下:
'clsCache.asp
。========================
'==begin:2004-6-2621:51:47
。==copyright:slightboy(C)1998-2004
。==email:slightboy@msn.com
。========================
。========================
'DimApplication(2)
。pplication(0)Counter計數(shù)器
。pplication(1)dateTime放置時間
。pplication(2)Content緩存內(nèi)容
PublicPREFIX
PublicPREFIX_LENGTH
PrivateSubClass_Initialize()
PREFIX="Cached:"
PREFIX_LENGTH=7
EndSub
PrivateSubClass_Terminate
EndSub
。гO置變量
PublicPropertyLetCache(ByRefKey,ByRefContent)
DimItem(2)
Item(0)=0
Item(1)=Now()
IF(IsObject(Content))Then
SetItem(2)=Content
Else
Item(2)=Content
EndIF
Application.Unlock
Application(PREFIX&Key)=Item
Application.Lock
EndProperty
。〕鲎兞坑嫈(shù)器++
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
。z查緩存對象是否存在
PublicPropertyGetExists(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
Exists=True
Else
Exists=False
EndIF
EndProperty
。У玫接嫈(shù)器數(shù)值
PublicPropertyGetCounter(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
Counter=Item(0)
EndIF
EndProperty
。гO置計數(shù)器時間
PublicPropertyLetdateTime(ByRefKey,ByRefSetdateTime)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
Item(1)=SetdateTime
EndIF
EndProperty
。У玫接嫈(shù)器時間
PublicPropertyGetdateTime(ByRefKey)
DimItem
Item=Application(PREFIX&Key)
IF(IsArray(Item))Then
dateTime=Item(1)
EndIF
EndProperty
'重置計數(shù)器
PublicSubResetCounter()
DimKey
DimItem
Application.Unlock
ForEachKeyinApplication.Contents
IF(Left(Key,PREFIX_LENGTH)=PREFIX)Then
Item=Application(Key)
Item(0)=0
Application(Key)=Item
EndIF
Next
Application.Lock
EndSub
。h除某以緩存
PublicSubClear(ByRefKey)
Application.Contents.Remove(PREFIX&Key)
EndSub
'清空沒有使用的緩存
PublicSubClearUnused()
DimKey,Keys,KeyLength,KeyIndex
ForEachKeyinApplication.Contents
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)
Application.Unlock
ForKeyIndex=1ToKeyLength
Application.Contents.Remove(Keys(KeyIndex))
Next
Application.Lock
EndSub
。蹇账芯彺
PublicSubClearAll()
DimKey,Keys,KeyLength,KeyIndex
ForEachKeyinApplication.Contents
IF(Left(Key,PREFIX_LENGTH)=PREFIX)Then
Keys=Keys&VBNewLine&Key
EndIF
Next
Keys=Split(Keys,VBNewLine)
KeyLength=UBound(Keys)
Application.Unlock
ForKeyIndex=1ToKeyLength
Application.Contents.Remove(Keys(KeyIndex))
Next
Application.Lock
EndSub
EndClass
slightboyn類例子SetWyd=NewJayCache
Wyd.dateTime("Page")=時間
IfWyd.Exists("Page")Then
Response.writeWyd.Cache("Page")'輸出
Else
Wyd.Cache("Page")=xxx寫入
Responxe.writexxx
EndIF
Wyd.Clear("page")'刪除緩存
【asp緩存類代碼】相關文章:
4.asp購物車代碼