CacheCls缓存的应用
代码如下:
<%
rem =================================================================
rem = 类:cachecls
rem = 说明:缓存的应用
rem = revision:1.01 beta
rem = 适用:对一些常用到,而又不常改变的数据放入缓存中,调用速度要比每次都要从数据库中读要快n陪
rem =================================================================
cachename = "rl"
class cachecls
private localcachename, cache_data
public property let name(byval vnewvalue)
localcachename = lcase(vnewvalue)
cache_data=application(cachename & "_" & localcachename)
end property
public property let value(byval vnewvalue)
dim n,i,newvaluearr
if localcachename<>"" then
n = countinstr(vnewvalue,"|")
newvaluearr = split(vnewvalue,"|",-1,1)
redim cache_data(n)
for i = 0 to n
cache_data(i) = newvaluearr(i)
next
application.lock
application(cachename & "_" & localcachename) = cache_data
application.unlock
else
response.write "设置缓存出错,或缓存名不能为空,请重新更新缓存"
response.end()
end if
end property
public property get value()
if localcachename<>"" then
if isarray(cache_data) then
value=cache_data
end if
else
response.write "设置缓存出错,或缓存名不能为空,请重新更新缓存"
response.end()
end if
end property
'取指定缓存中的值
public function getcachevalue(mycahename)
getcachevalue = application(cachename & "_" & mycahename)
end function
'取所有缓存名
public function getallcachename()
dim cacheobj
for each cacheobj in application.contents
getallcachename = getallcachename & cacheobj & ","
next
getallcachename = left(getallcachename,len(getallcachename)-1)
getallcachename = replace(getallcachename,cachename & "_","")
end function
'释放缓存
public sub delcahe(mycahename)
application.lock
application.contents.remove(cachename & "_" & mycahename)
application.unlock
end sub
'释放所有缓存
public sub removeallcache()
dim cachelist,i
cachelist=split(getallcachename(),",")
if ubound(cachelist)>0 then
for i=0 to ubound(cachelist)
delcahe cachelist(i)
next
end if
end sub
'统计字符char在str中出现的次数
private function countinstr(str,char)
countinstr = 0
dim i, charlen
charlen = len(char)
for i = 1 to len(str)
if mid(str, i, charlen) = char then countinstr = countinstr + 1
next
end function
end class
dim cachepro
set cachepro = new cachecls
'设置缓存“cexo255”和它的值:"cexo2551|cexo2552|cexo2553|cexo2554|cexo2555"
cachepro.name = "cexo255"
cachepro.value = "cexo2551|cexo2552|cexo2553|cexo2554|cexo2555"
'取当前缓存中的值
'cachearr = cachepro.value
cachepro.name = "wxf"
cachepro.value = "wxf"
cachepro.name = "dw"
cachepro.value = "dw"
'释放缓存cexo255
'cachepro.delcahe("cexo255")
'释放所有缓存
'cachepro.removeallcache
'取cexo255缓存中的值
cachearr = cachepro.getcachevalue("cexo255")
if isarray(cachearr) then
for i = 0 to ubound(cachearr)
response.write cachearr(i) & "
"
next
else
response.write "缓存被释放!!!"
end if
set cachepro = nothing
%>