ASP缓存类
来源:网络来源TAG:缓存浏览数: 日期:2010-8-20评论:
折叠ASP/Visual Basic Code复制内容到剪贴板
  1. <%    
  2. '**********************************************    
  3. 'vbs Cache类    
  4. ' 属性valid,是否可用,取值前判断    
  5. ' 属性name,cache名,新建对象后赋值    
  6. ' 方法add(值,到期时间),设置cache内容    
  7. ' 属性value,返回cache内容    
  8. ' 属性blempty,是否未设置值    
  9. ' 方法makeEmpty,释放内存,测试用    
  10. ' 方法equal(变量1),判断cache值是否和变量1相同    
  11. ' 方法expires(time),修改过期时间为time    
  12. ' 木鸟写的缓存类    
  13. '**********************************************    
  14.   
  15. class Cache    
  16. private obj 'cache内容    
  17. private expireTime '过期时间    
  18. private expireTimeName '过期时间application名    
  19. private cacheName 'cache内容application名    
  20. private path 'uri    
  21.   
  22. private sub class_initialize()    
  23. path=request.servervariables("url")    
  24. path=left(path,instrRev(path,"/"))    
  25. end sub    
  26.   
  27. private sub class_terminate()    
  28. end sub    
  29.   
  30. public property get blEmpty    
  31. '是否为空    
  32. if isempty(obj) then    
  33. blEmpty=true    
  34. else    
  35. blEmpty=false    
  36. end if    
  37. end property    
  38.   
  39. public property get valid    
  40. '是否可用(过期)    
  41. if isempty(obj) or not isDate(expireTime) then    
  42. valid=false    
  43. elseif CDate(expireTime)<now then    
  44. valid=false    
  45. else    
  46. valid=true    
  47. end if    
  48. end property    
  49.   
  50. public property let name(str)    
  51. '设置cache名    
  52. cacheName=str & path    
  53. obj=application(cacheName)    
  54. expireTimeName=str & "expires" & path    
  55. expireTime=application(expireTimeName)    
  56. end property    
  57.   
  58. public property let expires(tm)    
  59. '重设置过期时间    
  60. expireTime=tm    
  61. application.lock    
  62. application(expireTimeName)=expireTime    
  63. application.unlock    
  64. end property    
  65.   
  66. public sub add(var,expire)    
  67. '赋值    
  68. if isempty(var) or not isDate(expire) then    
  69. exit sub    
  70. end if    
  71. obj=var    
  72. expireTime=expire    
  73. application.lock    
  74. application(cacheName)=obj    
  75. application(expireTimeName)=expireTime    
  76. application.unlock    
  77. end sub    
  78.   
  79. public property get value    
  80. '取值    
  81. if isempty(obj) or not isDate(expireTime) then    
  82. value=null    
  83. elseif CDate(expireTime)<now then    
  84. value=null    
  85. else    
  86. value=obj    
  87. end if    
  88. end property    
  89.   
  90. public sub makeEmpty()    
  91. '释放application    
  92. application.lock    
  93. application(cacheName)=empty    
  94. application(expireTimeName)=empty    
  95. application.unlock    
  96. obj=empty    
  97. expireTime=empty    
  98. end sub    
  99.   
  100. public function equal(var2)    
  101. '比较    
  102. if typename(obj)<>typename(var2) then    
  103. equal=false    
  104. elseif typename(obj)="Object" then    
  105. if obj is var2 then    
  106. equal=true    
  107. else    
  108. equal=false    
  109. end if    
  110. elseif typename(obj)="Variant()" then    
  111. if join(obj,"^")=join(var2,"^") then    
  112. equal=true    
  113. else    
  114. equal=false    
  115. end if    
  116. else    
  117. if obj=var2 then    
  118. equal=true    
  119. else    
  120. equal=false    
  121. end if    
  122. end if    
  123. end function    
  124.   
  125. end class    
  126.   
  127. dim content,myCache    
  128. Set myCache = new Cache    
  129. myCache.name="sofoisndoffo" '定义缓存名称    
  130. if myCache.valid then '如果缓存有效    
  131. content=myCache.value '读取缓存内容    
  132. else    
  133. content="sosuo8.com测试" '大量内容,可以是非常耗时大量数据库查询记录集    
  134. myCache.add content,dateadd("n",1000,now) '将内容赋值给缓存,并设置缓存有效期是当前时间+1000分钟    
  135. end if    
  136. Response.Write(content)    
  137. 'myCache.makeEmpty()    
  138. set clsCache=nothing '释放对象    
  139. %>    
  140.   
  141. 详细出处参考:http://www.jb51.net/article/11146.htm  
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
新闻链接
下一篇文章:ASP语法高亮类代码
相关评论
正在加载评论列表...
评论表单加载中...