ASP实现缓存类无错版
程序员文章站
2022-10-27 19:46:58
<% '********************************************** 'vbs cache类 '&n...
<%
'**********************************************
'vbs cache类
' 属性valid,是否可用,取值前判断
' 属性name,cache名,新建对象后赋值
' 方法add(值,到期时间),设置cache内容
' 属性value,返回cache内容
' 属性blempty,是否未设置值
' 方法makeempty,释放内存,测试用
' 方法equal(变量1),判断cache值是否和变量1相同
' 方法expires(time),修改过期时间为time
' 木鸟写的缓存类
'**********************************************
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
dim content,mycache
set mycache = new cache
mycache.name="sofoisndoffo" '定义缓存名称
if mycache.valid then '如果缓存有效
content=mycache.value '读取缓存内容
else
content="sosuo8.com测试" '大量内容,可以是非常耗时大量数据库查询记录集
mycache.add content,dateadd("n",1000,now) '将内容赋值给缓存,并设置缓存有效期是当前时间+1000分钟
end if
response.write(content)
'mycache.makeempty()
set clscache=nothing '释放对象
%>
'**********************************************
'vbs cache类
' 属性valid,是否可用,取值前判断
' 属性name,cache名,新建对象后赋值
' 方法add(值,到期时间),设置cache内容
' 属性value,返回cache内容
' 属性blempty,是否未设置值
' 方法makeempty,释放内存,测试用
' 方法equal(变量1),判断cache值是否和变量1相同
' 方法expires(time),修改过期时间为time
' 木鸟写的缓存类
'**********************************************
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
dim content,mycache
set mycache = new cache
mycache.name="sofoisndoffo" '定义缓存名称
if mycache.valid then '如果缓存有效
content=mycache.value '读取缓存内容
else
content="sosuo8.com测试" '大量内容,可以是非常耗时大量数据库查询记录集
mycache.add content,dateadd("n",1000,now) '将内容赋值给缓存,并设置缓存有效期是当前时间+1000分钟
end if
response.write(content)
'mycache.makeempty()
set clscache=nothing '释放对象
%>
上一篇: 移动老总上公厕