asp缓存类
程序员文章站
2022-03-25 21:38:09
至于缓存的作用,我想我也不用再多说了,它的作用已经很明显,特别是对于信息量非常大或是全数据库页面的网站,他能很好地利用主机的内存资源,加速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.aspsky.net/
'**********************************************
class cache
private obj 'cache内容
private expiretime '过期时间
private expiretimename '过期时间application名
private cachename 'cache内容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)
'设置cache名
cachename=str & path
obj=application(cachename)
expiretimename=str & "expires" & path
expiretime=application(expiretimename)
end property
public property let expires(tm)
'重设置过期时间
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名,新建对象后赋值
' 方法add(值,到期时间),设置cache内容
' 属性value,返回cache内容
' 属性blempty,是否未设置值
' 方法makeempty,释放内存,
' 方法delcahe ,删除内存
' 方法equal(变量1),判断cache值是否和变量1相同
' 方法expires(time),修改过期时间为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) '写入缓存 xxx.add 内容,过期时间
response.write boardjumplist '输出
end if
mycache.makeempty() 释放内存
mycache.delcahe() 删除缓存
迷城浪子写的
class cls_cache
rem ==================使用说明====================
rem = 本类模块是动网先锋原创,作者:迷城浪子。如采用本类模块,请不要去掉这个说明。这段注释不会影响执行的速度。
rem = 作用:缓存和缓存管理类
rem = 公有变量:reloadtime 过期时间(单位为分钟)缺省值为14400
rem = maxcount 缓存对象的最大值,超过则自动删除使用次数少的对象。缺省值为300
rem = cachename 缓存组的总名称,缺省值为"dvbbs",如果一个站点中有超过一个缓存组,则需要外部改变这个值。
rem = 属性:name 定义缓存对象名称,只写属性。
rem = 属性:value 读取和写入缓存数据。
rem = 函数:objisempty()判断当前缓存是否过期。
rem = 方法:delcahe(mycahename)手工删除一个缓存对象,参数是缓存对象的名称。
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 '定义过期时间 (以分钟为单会)
wydcache.cachename="pages" '定义缓存名
if wydcache.objisempty() then ''判断是否可用(包括过期,与是否为空值)
response.write wydcache.value
else
..................
boardjumplist=xxx
wydcache.value=boardjumplist '写入内容
response.write boardjumplist
end if
mycache.delcahe("缓存名") 删除缓存
slightboy 写的 '========================
'clscache.asp
'========================
'== begin : 2004-6-26 21:51:47
'== copyright : slightboy (c)1998-2004
'== email : slightboy@msn.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")'删除缓存
木鸟写的
复制代码 代码如下:
'**********************************************
' vbs cache类
'
' 属性valid,是否可用,取值前判断
' 属性name,cache名,新建对象后赋值
' 方法add(值,到期时间),设置cache内容
' 属性value,返回cache内容
' 属性blempty,是否未设置值
' 方法makeempty,释放内存,测试用
' 方法equal(变量1),判断cache值是否和变量1相同
' 方法expires(time),修改过期时间为time
' 木鸟 2002.12.24
' http://www.aspsky.net/
'**********************************************
class cache
private obj 'cache内容
private expiretime '过期时间
private expiretimename '过期时间application名
private cachename 'cache内容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)
'设置cache名
cachename=str & path
obj=application(cachename)
expiretimename=str & "expires" & path
expiretime=application(expiretimename)
end property
public property let expires(tm)
'重设置过期时间
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名,新建对象后赋值
' 方法add(值,到期时间),设置cache内容
' 属性value,返回cache内容
' 属性blempty,是否未设置值
' 方法makeempty,释放内存,
' 方法delcahe ,删除内存
' 方法equal(变量1),判断cache值是否和变量1相同
' 方法expires(time),修改过期时间为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) '写入缓存 xxx.add 内容,过期时间
response.write boardjumplist '输出
end if
mycache.makeempty() 释放内存
mycache.delcahe() 删除缓存
迷城浪子写的
复制代码 代码如下:
class cls_cache
rem ==================使用说明====================
rem = 本类模块是动网先锋原创,作者:迷城浪子。如采用本类模块,请不要去掉这个说明。这段注释不会影响执行的速度。
rem = 作用:缓存和缓存管理类
rem = 公有变量:reloadtime 过期时间(单位为分钟)缺省值为14400
rem = maxcount 缓存对象的最大值,超过则自动删除使用次数少的对象。缺省值为300
rem = cachename 缓存组的总名称,缺省值为"dvbbs",如果一个站点中有超过一个缓存组,则需要外部改变这个值。
rem = 属性:name 定义缓存对象名称,只写属性。
rem = 属性:value 读取和写入缓存数据。
rem = 函数:objisempty()判断当前缓存是否过期。
rem = 方法:delcahe(mycahename)手工删除一个缓存对象,参数是缓存对象的名称。
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 '定义过期时间 (以分钟为单会)
wydcache.cachename="pages" '定义缓存名
if wydcache.objisempty() then ''判断是否可用(包括过期,与是否为空值)
response.write wydcache.value
else
..................
boardjumplist=xxx
wydcache.value=boardjumplist '写入内容
response.write boardjumplist
end if
mycache.delcahe("缓存名") 删除缓存
slightboy 写的 '========================
复制代码 代码如下:
'clscache.asp
'========================
'== begin : 2004-6-26 21:51:47
'== copyright : slightboy (c)1998-2004
'== email : slightboy@msn.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")'删除缓存