[原创]缓存技术
<%
Class clsCache
private cache
private cacheNames
private expireTime
private expireTimeName
private path
private sub class_initialize()
path=request.servervariables("url")
path=left(path,instrRev(path,"/"))
end sub
private sub class_terminate()
end sub
Public Property Get Version
Version="先锋缓存类 Version 2004"
End Property
public property get valid '读取缓存是否有效/属性
if isempty(cache) or (not isdate(expireTime) or CDate(expireTime)<now) then
valid=false
else
valid=true
end if
end property
public property get value '读取当前缓存内容/属性
if isempty(cache) or (not isDate(expireTime)) then
value=null
elseif CDate(expireTime)<now then
value=null
else
value=cache
end if
end property
public property let name(str) '设置缓存名称/属性
cacheNames=str&path
cache=application(cacheNames)
expireTimeName=str&"expire"&path
expireTime=application(expireTimeName)
end property
public property let expire(tm) '设置缓存过期时间/属性
expireTime=tm
application.Lock()
application(expireTimeName)=expireTime
application.UnLock()
end property
public sub add(varCache,varExpireTime) '对缓存赋值/方法
if isempty(varCache) or not isDate(varExpireTime) then
exit sub
end if
cache=varCache
expireTime=varExpireTime
application.lock
application(cacheNames)=cache
application(expireTimeName)=expireTime
application.unlock
end sub
public sub clean() '释放缓存/方法
application.lock
application(cacheNames)=empty
application(expireTimeName)=empty
application.unlock
cache=empty
expireTime=empty
End sub
public function verify(varcache2) '比较缓存值是否相同/方法——返回是或否
if typename(cache)<>typename(varcache2) then
verify=false
elseif typename(cache)="Object" then
if cache is varcache2 then
verify=true
else
verify=false
end if
elseif typename(cache)="Variant()" then
if join(cache,"^")=join(varcache2,"^") then
verify=true
else
verify=false
end if
else
if cache=varcache2 then
verify=true
else
verify=false
end if
end if
end function
ENd Class
%>
<%
dim content,myCache,con
set myCache = New clsCache
myCache.name="test1" '定义缓存名称
if myCache.valid then '如果缓存有效
content=myCache.value '读取缓存内容
else
content="大家要努力工作啊"&int(rnd*9)+1 '大量内容,可以是非常耗时大量数据库查询记录集
myCache.add content,dateadd("s",10,now) '将内容赋值给缓存,并设置缓存有效期是当前时间+10秒
end if
if mycache.verify("大家要工作啊") then
response.Write("yes")
else
response.Write("no")
end if
set myCache=nothing '释放对象
response.Write(content)
%>
注意,不能
content="大家要努力工作啊"&int(rnd*9)+1
con="11"
myCache.add content,dateadd("s",10,now)
myCache.add con,dateadd("s",10,now)
那就会出现两个同样的缓存,解决方法是要分开写。