ASP实现缓存类无错版

2018-09-06 10:34

阅读:655

  <%
**********************************************
vbsCache类
属性valid,是否可用,取值前判断
属性name,cache名,新建对象后赋值
方法add(值,到期时间),设置cache内容
属性value,返回cache内容
属性blempty,是否未设置值
方法makeEmpty,释放内存,测试用
方法equal(变量1),判断cache值是否和变量1相同
方法expires(time),修改过期时间为time
木鸟写的缓存类
**********************************************

classCache
privateobjcache内容
privateexpireTime过期时间
privateexpireTimeName过期时间application名
privatecacheNamecache内容application名
privatepathuri

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)=Objectthen
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

dimcontent,myCache
SetmyCache=newCache
ifmyCache.validthen如果缓存有效
content=myCache.value读取缓存内容
else
myCache.addcontent,dateadd(n,1000,now)将内容赋值给缓存,并设置缓存有效期是当前时间+1000分钟
endif
Response.Write(content)
myCache.makeEmpty()
setclsCache=nothing释放对象
%>


评论


亲,登录后才可以留言!