asp缓存类代码
至于缓存的作用,我想我也不用再多说了,它的作用已经很明显,特别是对于信息量非常大或是全数据库页面的网站,他能很好地利用主机的内存资源,加速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
'http://www.aspsky.net/
'***
classCache
privateobj'cache内容
privateexpireTime'过期时间
privateexpireTimeName'过期时间application名
privatecacheName'cache内容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)
'设置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)
'赋值
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()
'释放application
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类
'
'属性valid,是否可用,取值前判断
'属性name,cache名,新建对象后赋值
'方法add(值,到期时间),设置cache内容
'属性value,返回cache内容
'属性blempty,是否未设置值
'方法makeEmpty,释放内存,
'方法DelCahe,删除内存
'方法equal(变量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内容,过期时间
response.writeBoardJumpList'输出
endif
myCache.makeEmpty()释放内存
mycache.DelCahe()删除缓存
迷城浪子写的
复制代码 代码如下:
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.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'写入内容
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)
'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
Application.Unlock
Application(PREFIX&Key)=Item
Application.Lock
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
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
'删除某以缓存
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缓存类代码】相关文章:
3.asp购物车代码