1. 木鸟写的
  2. '**********************************************
  3. ' vbs Cache
  4. '
  5. ' 属性valid,是否可用,取值前判断
  6. ' 属性name,cache名,新建对象后赋值
  7. ' 方法add(值,到期时间),设置cache内容
  8. ' 属性value,返回cache内容
  9. ' 属性blempty,是否未设置值
  10. ' 方法makeEmpty,释放内存,测试用
  11. ' 方法equal(变量1),判断cache值是否和变量1相同
  12. ' 方法expires(time),修改过期时间为time
  13. ' 木鸟 2002.12.24
  14. ' http://www.aspsky.net/
  15. '**********************************************
  16. class Cache
  17. private obj 'cache内容
  18. private expireTime '过期时间
  19. private expireTimeName '过期时间application名
  20. private cacheName 'cache内容application
  21. private path 'uri
  22.  
  23. private sub class_initialize()
  24. path=request.servervariables("url")
  25. path=left(path,instrRev(path,"/"))
  26. end sub
  27.  
  28. private sub class_terminate()
  29. end sub
  30.  
  31. public property get blEmpty
  32. '是否为空
  33. if isempty(obj) then
  34. blEmpty=true
  35. else
  36. blEmpty=false
  37. end if
  38. end property
  39.  
  40. public property get valid
  41. '是否可用(过期)
  42. if isempty(obj) or not isDate(expireTime) then
  43. valid=false
  44. elseif CDate(expireTime)<now then
  45. valid=false
  46. else
  47. valid=true
  48. end if
  49. end property
  50.  
  51. public property let name(str)
  52. '设置cache
  53. cacheName=str & path
  54. obj=application(cacheName)
  55. expireTimeName=str & "expires" & path
  56. expireTime=application(expireTimeName)
  57. end property
  58.  
  59. public property let expires(tm)
  60. '重设置过期时间
  61. expireTime=tm
  62. application.lock
  63. application(expireTimeName)=expireTime
  64. application.unlock
  65. end property
  66.  
  67. public sub add(var,expire)
  68. '赋值
  69. if isempty(var) or not isDate(expire) then
  70. exit sub
  71. end if
  72. obj=var
  73. expireTime=expire
  74. application.lock
  75. application(cacheName)=obj
  76. application(expireTimeName)=expireTime
  77. application.unlock
  78. end sub
  79.  
  80. public property get value
  81. '取值
  82. if isempty(obj) or not isDate(expireTime) then
  83. value=null
  84. elseif CDate(expireTime)<now then
  85. value=null
  86. else
  87. value=obj
  88. end if
  89. end property
  90.  
  91. public sub makeEmpty()
  92. '释放application
  93. application.lock
  94. application(cacheName)=empty
  95. application(expireTimeName)=empty
  96. application.unlock
  97. obj=empty
  98. expireTime=empty
  99. end sub
  100.  
  101. public function equal(var2)
  102. '比较
  103. if typename(obj)<>typename(var2) then
  104. equal=false
  105. elseif typename(obj)="Object" then
  106. if obj is var2 then
  107. equal=true
  108. else
  109. equal=false
  110. end if
  111. elseif typename(obj)="Variant()" then
  112. if join(obj,"^")=join(var2,"^") then
  113. equal=true
  114. else
  115. equal=false
  116. end if
  117. else
  118. if obj=var2 then
  119. equal=true
  120. else
  121. equal=false
  122. end if
  123. end if
  124. end function
  125. end class
  126.  
  127. 木鸟 类例子 vbs Cache类
  128.  
  129. '
  130. ' 属性valid,是否可用,取值前判断
  131. ' 属性namecache名,新建对象后赋值
  132. ' 方法add(值,到期时间),设置cache内容
  133. ' 属性value,返回cache内容
  134. ' 属性blempty,是否未设置值
  135. ' 方法makeEmpty,释放内存,
  136. ' 方法DelCahe ,删除内存
  137. ' 方法equal(变量1),判断cache值是否和变量1相同
  138. ' 方法expires(time),修改过期时间为time
  139. ' 用法
  140.  
  141. set myCache=New Cache
  142. myCache.name="BoardJumpList" '定义缓存名
  143. if myCache.valid then '判断是否可用(包括过期,与是否为空值)
  144. response.write myCache.value '输出
  145. else
  146. ................
  147. BoardJumpList=xxx
  148. myCache.add BoardJumpList,dateadd("n",60,now) '写入缓存 xxx.add 内容,过期时间
  149. response.write BoardJumpList '输出
  150. end if
  151. myCache.makeEmpty() 释放内存
  152. mycache.DelCahe() 删除缓存
  153.  
  154. ==========================================================================
  155.  
  156. 迷城浪子写的 Class Cls_Cache
  157.  
  158. Rem ==================使用说明====================
  159. Rem = 本类模块是动网先锋原创,作者:迷城浪子。如采用本类模块,请不要去掉这个说明。这段注释不会影响执行的速度。
  160. Rem = 作用:缓存和缓存管理类
  161. Rem = 公有变量:Reloadtime 过期时间(单位为分钟)缺省值为14400
  162. Rem = MaxCount 缓存对象的最大值,超过则自动删除使用次数少的对象。缺省值为300
  163. Rem = CacheName 缓存组的总名称,缺省值为"Dvbbs",如果一个站点中有超过一个缓存组,则需要外部改变这个值。
  164. Rem = 属性:Name 定义缓存对象名称,只写属性。
  165. Rem = 属性:value 读取和写入缓存数据。
  166. Rem = 函数:ObjIsEmpty()判断当前缓存是否过期。
  167. Rem = 方法:DelCahe(MyCaheName)手工删除一个缓存对象,参数是缓存对象的名称。
  168. Rem ========================
  169. Public Reloadtime,MaxCount,CacheName
  170. Private LocalCacheName,CacheData,DelCount
  171. Private Sub Class_Initialize()
  172. Reloadtime=14400
  173. CacheName="Dvbbs"
  174. End Sub
  175. Private Sub SetCache(SetName,NewValue)
  176. Application.Lock
  177. Application(SetName) = NewValue
  178. Application.unLock
  179. End Sub
  180. Private Sub makeEmpty(SetName)
  181. Application.Lock
  182. Application(SetName) = Empty
  183. Application.unLock
  184. End Sub
  185. Public Property Let Name(ByVal vNewValue)
  186. LocalCacheName=LCase(vNewValue)
  187. End Property
  188. Public Property Let Value(ByVal vNewValue)
  189. If LocalCacheName<>"" Then
  190. CacheData=Application(CacheName&"_"&LocalCacheName)
  191. If IsArray(CacheData) Then
  192. CacheData(0)=vNewValue
  193. CacheData(1)=Now()
  194. Else
  195. ReDim CacheData(2)
  196. CacheData(0)=vNewValue
  197. CacheData(1)=Now()
  198. End If
  199. SetCache CacheName&"_"&LocalCacheName,CacheData
  200. Else
  201. Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
  202. End If
  203. End Property
  204. Public Property Get Value()
  205. If LocalCacheName<>"" Then
  206. CacheData=Application(CacheName&"_"&LocalCacheName)
  207. If IsArray(CacheData) Then
  208. Value=CacheData(0)
  209. Else
  210. Err.Raise vbObjectError + 1, "DvbbsCacheServer", " The CacheData Is Empty."
  211. End If
  212. Else
  213. Err.Raise vbObjectError + 1, "DvbbsCacheServer", " please change the CacheName."
  214. End If
  215. End Property
  216. Public Function ObjIsEmpty()
  217. ObjIsEmpty=True
  218. CacheData=Application(CacheName&"_"&LocalCacheName)
  219. If Not IsArray(CacheData) Then Exit Function
  220. If Not IsDate(CacheData(1)) Then Exit Function
  221. If DateDiff("s",CDate(CacheData(1)),Now()) < 60*Reloadtime Then
  222. ObjIsEmpty=False
  223. End If
  224. End Function
  225. Public Sub DelCahe(MyCaheName)
  226. makeEmpty(CacheName&"_"&MyCaheName)
  227. End Sub
  228. End Class
  229.  
  230. 迷城浪子 类例子
  231.  
  232. Set WydCache=New Cls_Cache
  233. WydCache.Reloadtime=0.5 '定义过期时间 (以分钟为单会)
  234. WydCache.CacheName="pages" '定义缓存名
  235. IF WydCache.ObjIsEmpty() Then ''判断是否可用(包括过期,与是否为空值)
  236. Response.write WydCache.Value
  237. Else
  238. ..................
  239. BoardJumpList=xxx
  240. WydCache.Value=BoardJumpList '写入内容
  241. Response.write BoardJumpList
  242. End if
  243.  
  244. mycache.DelCahe("缓存名") 删除缓存
  245.  
  246. ==========================================================================
  247.  
  248. slightboy 写的
  249.  
  250. '========================
  251. 'clsCache.asp
  252. '========================
  253. '== begin : 2004-6-26 21:51:47
  254. '== copyright : slightboy (C)1998-2004
  255. '== email : slightboy@msn.com
  256. '========================
  257. '========================
  258. ' Dim Application(2)
  259. ' Application(0) Counter 计数器
  260. ' Application(1) dateTime 放置时间
  261. ' Application(2) Content 缓存内容
  262.  
  263. Public PREFIX
  264. Public PREFIX_LENGTH
  265.  
  266. Private Sub Class_Initialize()
  267. PREFIX = "Cached:"
  268. PREFIX_LENGTH = 7
  269. End Sub
  270. Private Sub Class_Terminate
  271. End Sub
  272. ' 设置变量
  273. Public Property Let Cache(ByRef Key, ByRef Content)
  274. Dim Item(2)
  275. Item(0) = 0
  276. Item(1) = Now()
  277. IF (IsObject(Content)) Then
  278. Set Item(2) = Content
  279. Else
  280. Item(2) = Content
  281. End IF
  282. Application.Unlock
  283. Application(PREFIX & Key) = Item
  284. Application.Lock
  285. End Property
  286. ' 取出变量 计数器++
  287. Public Property Get Cache(ByRef Key)
  288. Dim Item
  289. Item = Application(PREFIX & Key)
  290. IF (IsArray(Item)) Then
  291. IF (IsObject(Item)) Then
  292. Set Cache = Item(2)
  293. Else
  294. Cache = Item(2)
  295. End IF
  296. Application(PREFIX & Key)(0) = Application(PREFIX & Key)(0) + 1
  297. Else
  298. Cache = Empty
  299. End IF
  300. End Property
  301. ' 检查缓存对象是否存在
  302. Public Property Get Exists(ByRef Key)
  303. Dim Item
  304. Item = Application(PREFIX & Key)
  305. IF (IsArray(Item)) Then
  306. Exists = True
  307. Else
  308. Exists = False
  309. End IF
  310. End Property
  311. ' 得到计数器数值
  312. Public Property Get Counter(ByRef Key)
  313. Dim Item
  314. Item = Application(PREFIX & Key)
  315. IF (IsArray(Item)) Then
  316. Counter = Item(0)
  317. End IF
  318. End Property
  319.  
  320. ' 设置计数器时间
  321. Public Property Let dateTime(ByRef Key, ByRef SetdateTime)
  322. Dim Item
  323. Item = Application(PREFIX & Key)
  324. IF (IsArray(Item)) Then
  325. Item(1) = SetdateTime
  326. End IF
  327. End Property
  328. ' 得到计数器时间
  329. Public Property Get dateTime(ByRef Key)
  330. Dim Item
  331. Item = Application(PREFIX & Key)
  332. IF (IsArray(Item)) Then
  333. dateTime = Item(1)
  334. End IF
  335. End Property
  336.  
  337. ' 重置计数器
  338. Public Sub ResetCounter()
  339. Dim Key
  340. Dim Item
  341. Application.Unlock
  342. For Each Key in Application.Contents
  343. IF (Left(Key, PREFIX_LENGTH) = PREFIX) Then
  344. Item = Application(Key)
  345. Item(0) = 0
  346. Application(Key) = Item
  347. End IF
  348. Next
  349. Application.Lock
  350. End Sub
  351. ' 删除某以缓存
  352. Public Sub Clear(ByRef Key)
  353. Application.Contents.Remove(PREFIX & Key)
  354. End Sub
  355. ' 清空没有使用的缓存
  356. Public Sub ClearUnused()
  357. Dim Key, Keys, KeyLength, KeyIndex
  358. For Each Key in Application.Contents
  359. IF (Left(Key, PREFIX_LENGTH) = PREFIX) Then
  360. IF (Application(Key)(0) = 0) Then
  361. Keys = Keys & VBNewLine & Key
  362. End IF
  363. End IF
  364. Next
  365. Keys = Split(Keys, VBNewLine)
  366. KeyLength = UBound(Keys)
  367. Application.Unlock
  368. For KeyIndex = 1 To KeyLength
  369. Application.Contents.Remove(Keys(KeyIndex))
  370. Next
  371. Application.Lock
  372. End Sub
  373. ' 清空所有缓存
  374. Public Sub ClearAll()
  375. Dim Key, Keys, KeyLength, KeyIndex
  376. For Each Key in Application.Contents
  377. IF (Left(Key, PREFIX_LENGTH) = PREFIX) Then
  378. Keys = Keys & VBNewLine & Key
  379. End IF
  380. Next
  381. Keys = Split(Keys, VBNewLine)
  382. KeyLength = UBound(Keys)
  383. Application.Unlock
  384. For KeyIndex = 1 To KeyLength
  385. Application.Contents.Remove(Keys(KeyIndex))
  386. Next
  387. Application.Lock
  388. End Sub
  389.  
  390. End Class
  391.  
  392. slightboyn 类例子
  393.  
  394. Set Wyd=New JayCache
  395. Wyd.dateTime("Page")=时
  396. If Wyd.Exists("Page") Then
  397. Response.write Wyd.Cache("Page") '输出
  398. Else
  399. Wyd.Cache("Page")=xxx 写入
  400. Responxe.write xxx
  401. End IF
  402. Wyd.Clear("page")'删除缓存
  403.  
  404. ==========================================================================
  405.  
  406. 无惧缓存类 V1.0
  407.  
  408. Cache_class.asp
  409.  
  410. <%
  411. ' ============================================
  412. ' 转发时请保留此声明信息,这段声明不并会影响你的速度!
  413. ' 类名:无惧缓存类 V1.0
  414. ' 作者:梁无惧
  415. ' 网站:http://www.25CN.com
  416. ' 电子邮件:yjlrb@25CN.com
  417. ' 版权声明:版权所有,源代码公开,各种用途均可免费使用,但是修改后必须把修改后的文件
  418. ' 发送一份给作者.
  419. ' ============================================
  420. ' 用途:用于常用数据的缓存,以减少执行,加快速度,但是由于使用Application来存贮数据,有可能对服务器造成负担
  421. ' 类名 Cache_Class
  422. ' 方法 NoCache(函数名,关键字) 测试该缓存是否有效
  423. ' 属性 Value 如果缓存无效,使用该属性设置缓存,如果缓存,则使用该属性读取缓存
  424. ' 例子
  425. ' Dim Cache
  426. ' Set Cache = New Cache_Class
  427. ' if Cache.NoCache("getname(a)","username") Then Cache.Value=getname(a)
  428. ' Response.Write Cache.Value
  429. ' 注意:每次使用前都需要用NoCache方法来测试,否则无法保证的取得的值是否为当前设置的函数名
  430. ' 技巧:函数名用于识别,当有数据改变时,只需直接调用函数SetCacheKey(关键字)即可以刷新缓存,这样可保存缓存随数据的改变而重新缓存
  431. ' 默认建立Cache实例,可以在程序中直接调用而不需要事先创建
  432. ' ============================================
  433.  
  434. Class Cache_Class
  435. Dim Cache_Name, Cache_Key, Version, Cache_Value
  436.  
  437. Function NoCache(FunName, Key)
  438. Dim NoIn
  439. Cache_Name = FunName
  440. Cache_Key = Key
  441. Cache_Value = Application("Cache_" & Cache_Name)
  442. NoIn = True
  443. If IsArray(Cache_Value) Then If Application("CacheKey_" & Cache_Key) = Cache_Value(0) Then NoIn = False
  444. NoCache = NoIn
  445. End Function
  446.  
  447. Property Get Value()
  448. Value = Cache_Value(1)
  449. End Property
  450.  
  451. Property Let Value(Val)
  452. ReDim Cache_Value(1)
  453. Cache_Value(0) = Application("CacheKey_" & Cache_Key)
  454. Cache_Value(1) = Val
  455. Application("Cache_" & Cache_Name) = Cache_Value
  456. End Property
  457.  
  458. End Class
  459.  
  460. Function SetCacheKey(Key)
  461. Application("CacheKey_" & Key) = Timer
  462. End Function
  463.  
  464. Dim Cache
  465. Set Cache = New Cache_Class
  466. %>
  467.  
  468. ==========================================================================
  469.  
  470. ASPXML缓存类,代替了Application
  471.  
  472. <%
  473. '=========================================
  474. ' ClassName: RyM_ClsCache
  475. ' Version: 1.0
  476. ' Date: 2006-8-2
  477. ' Author: 网海の求生
  478. '=========================================
  479. ' 调用说明:
  480. ' Set CC = New RyM_ClsCache '创建对象
  481. ' CC.CreateXmlObj "Temp.xml","/ROYAH_CACHE"
  482. ' CC.Name = "CA" '设置缓存名
  483. ' If CC.IsXmlObj() Then '如果存在缓存则
  484. ' Temp = CC.Value '直接xml中读取值
  485. ' Else 否则
  486. ' Temp = "要缓存的内容,只能是字符串"
  487. ' CC.Value = Temp '把要缓存的值写入xml
  488. ' End If
  489. ' Set CC = Nothing '释放对象
  490. ' 变量Temp就是经过缓存后的字符串值了
  491. '=========================================
  492. Class RyM_ClsCache
  493. Public Reloadtime
  494. Private XmlDom, XmlDoc, XmlNode, XmlAttr, AttrTime
  495. Private CacheName, LocalCacheName, XmlPath
  496. Private Sub Class_Initialize()
  497. Reloadtime = 14400
  498. End Sub
  499. Private Sub Class_Terminate()
  500. Close()
  501. End Sub
  502. '新建文本文件
  503. Private Function SaveToFile(ByVal strBody,ByVal SavePath)
  504. Set ObjStream = Server.CreateObject("ADODB.Stream")
  505. ObjStream.Open
  506. ObjStream.Type = 2
  507. ObjStream.Charset = "GB2312"
  508. ObjStream.WriteText strBody
  509. ObjStream.SaveToFile SavePath,2
  510. ObjStream.Close
  511. Set ObjStream = Nothing
  512. End Function
  513. '创建Xml对象
  514. Public Sub CreateXmlObj(ByVal XmlName, ByVal ChName)
  515. Set XmlDom = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
  516. XmlPath = Server.MapPath(XmlName)
  517. CacheName = ChName
  518. If Not XmlDom.Load(XmlPath) Then '如果指定的缓存文件不存在则自动新建
  519. SaveToFile "<?xml version=""1.0"" encoding=""GB2312""?><ROYAH_CACHE></ROYAH_CACHE>",XmlPath
  520. XmlDom.Load(XmlPath)
  521. End If
  522. End Sub
  523. '设置返回数据节点名
  524. Public Property Let Name(ByVal vNewValue)
  525. LocalCacheName = vNewValue
  526. If LocalCacheName <> "" Then
  527. Set XmlDoc = XmlDom.documentElement.selectSingleNode(CacheName & "/" & LocalCacheName)
  528. End If
  529. End Property
  530. '设置当前节点值
  531. Public Property Let Value(ByVal vNewValue)
  532. If (XmlDoc Is Nothing) Then
  533. Set XmlDoc = XmlDom.documentElement.selectSingleNode(CacheName)
  534. Set XmlNode = XmlDom.createElement(LocalCacheName)
  535. Set XmlAttr = XmlDom.createAttribute("Time")
  536. XmlNode.Text = vNewValue
  537. XmlAttr.Text = Now()
  538. XmlDoc.AppendChild(XmlNode)
  539. XmlNode.setAttributeNode XmlAttr
  540. XmlDom.Save(XmlPath)
  541. Else
  542. XmlDoc.Text = vNewValue
  543. Set AttrTime = XmlDoc.selectSingleNode("./@Time")
  544. AttrTime.Text = Now()
  545. XmlDom.Save(XmlPath)
  546. End If
  547. End Property
  548. '返回当前节点值
  549. Public Property Get Value()
  550. If Not (XmlDoc Is Nothing) Then
  551. Value = XmlDoc.Text
  552. End If
  553. End Property
  554. '移除当前节点
  555. Public Sub Remove()
  556. If Not (XmlDoc Is Nothing) Then
  557. XmlDoc.ParentNode.RemoveChild(XmlDoc)
  558. XmlDom.Save(XmlPath)
  559. End If
  560. End Sub
  561. '检测当前节点是否存在
  562. Public Function IsXmlObj()
  563. IsXmlObj = False
  564. If Not (XmlDoc Is Nothing) Then
  565. IsXmlObj = True
  566. Set AttrTime = XmlDoc.selectSingleNode("./@Time")
  567. If DateDiff("s",CDate(AttrTime.Text),Now()) > (60*Reloadtime) Then IsXmlObj = False
  568. End If
  569. End Function
  570. '释放全部对象
  571. Public Sub Close()
  572. If IsObject(XmlDom) Then Set XmlDom = Nothing
  573. If IsObject(XmlDoc) Then Set XmlDoc = Nothing
  574. If IsObject(XmlNode) Then Set XmlNode = Nothing
  575. If IsObject(XmlAttr) Then Set XmlAttr = Nothing
  576. If IsObject(AttrTime) Then Set XmlAttr = Nothing
  577. End Sub
  578. End Class
  579. %>
  580.  
  581. ==========================================================================
  582.  
  583. '/******************************
  584. '类名:XmlCache
  585. '名称:xml缓存类
  586. '日期:2007-12-15
  587. '作者:西楼冷月
  588. '网址:www.xilou.net | www.chinaCMS.org
  589. '描述:缓存名称不区分大小写,也可以是中文,当是字母时会以小写状态存储
  590. '版权:
  591. '******************************
  592. '最后修改:2007-12-15
  593. '修改次数:0
  594. '修改说明:
  595. '目前版本:v1.0
  596. '***************XmlCache属性***************
  597. 'Title: xml文档标题
  598. 'Creator: xml文档创建人
  599. 'DateCreated: xml文档创建时间
  600. 'Description: xml文档说明
  601. 'Encoding: xml文档编码
  602.  
  603. 'Item: 设置或返回某个缓存的值,可读写
  604. 'ItemInfo: 返回数组,某个缓存的所有属性:名称,创建时间,过期时间截,值
  605. ' 如果不存在则返回一个空值的数组
  606. 'Keys: 返回所有的缓存名称,组成一个数组,只读
  607. 'Items: 返回所有缓存的数组,只读
  608.  
  609. 'Count: 缓存个数
  610. 'Xml: 返回整份xml文档
  611. 'IsAutoUpdate: 当在内存中修改xml时是否自动更新到该xml文件,默认是否False
  612.  
  613. '***************XmlCache方法***************
  614. '---xml缓存文档的操作
  615. 'Load(xmlFile): 加载xml缓存文档
  616. 'Create(xmlFile): 创建xml缓存文档
  617. 'Save(): 保存一份xml缓存文档
  618. 'SaveAs(xmlFile): 将xml缓存文档另存为
  619. 'DeleteFile(): 删除xml缓存文档
  620. '---缓存添加:
  621. 'Add(key,value): 添加一个缓存,失败返回false(比如:已经存在该key),成功返回true
  622. 'AddFull(key,value,s):添加一个缓存,包括名称,值,过期时间截
  623. '---缓存更新:
  624. 'Update(key,value): 更新一个缓存值,如果缓存存在并更新成功返回True,否则返回False
  625. 'UpdateExpires(key,s):更新一个缓存的过期时间截,如果缓存存在并更新成功返回True,否则返回False
  626. '---缓存删除:
  627. 'Remove(key): 删除一个缓存,如果该缓存名称存在而且成功删除则返回True否则返回False
  628. 'RemoveAll(): 删除所有缓存,返回True或False
  629. 'DeleteAll(): 删除所有过期的缓存,返回TrueFalse(表示没有过期的缓存)
  630. '---缓存读取:
  631. '可以使用Item,ItemInfo,Keys,Items属性操作
  632. '---缓存检查:
  633. 'Exists(key): 检查一个缓存是否存在
  634. 'CheckExpires(key): 检查一个缓存是否已经过期,是返回True,否返回False
  635. '******************************/
  636.  
  637. 程序代码
  638.  
  639. <%
  640. '/******************************
  641. '类名:XmlCache
  642. '名称:xml缓存类
  643. '日期:2007-12-15
  644. '作者:西楼冷月
  645. '网址:www.xilou.net | www.chinaCMS.org
  646. '描述:缓存名称不区分大小写,也可以是中文,当是字母时会以小写状态存储
  647. '版权:
  648. '******************************
  649. '最后修改:2007-12-15
  650. '修改次数:0
  651. '修改说明:
  652. '目前版本:v1.0
  653. '***************XmlCache属性***************
  654. 'Title: xml文档标题
  655. 'Creator: xml文档创建人
  656. 'DateCreated: xml文档创建时间
  657. 'Description: xml文档说明
  658. 'Encoding: xml文档编码
  659.  
  660. 'Item: 设置或返回某个缓存的值,可读写
  661. 'ItemInfo: 返回数组,某个缓存的所有属性:名称,创建时间,过期时间截,值
  662. ' 如果不存在则返回一个空值的数组
  663. 'Keys: 返回所有的缓存名称,组成一个数组,只读
  664. 'Items: 返回所有缓存的数组,只读
  665.  
  666. 'Count: 缓存个数
  667. 'Xml: 返回整份xml文档
  668. 'IsAutoUpdate: 当在内存中修改xml时是否自动更新到该xml文件,默认是否False
  669.  
  670. '***************XmlCache方法***************
  671. '---xml缓存文档的操作
  672. 'Load(xmlFile): 加载xml缓存文档
  673. 'Create(xmlFile): 创建xml缓存文档
  674. 'Save(): 保存一份xml缓存文档
  675. 'SaveAs(xmlFile): 将xml缓存文档另存为
  676. 'DeleteFile(): 删除xml缓存文档
  677. '---缓存添加:
  678. 'Add(key,value): 添加一个缓存,失败返回false(比如:已经存在该key),成功返回true
  679. 'AddFull(key,value,s):添加一个缓存,包括名称,值,过期时间截
  680. '---缓存更新:
  681. 'Update(key,value): 更新一个缓存值,如果缓存存在并更新成功返回True,否则返回False
  682. 'UpdateExpires(key,s):更新一个缓存的过期时间截,如果缓存存在并更新成功返回True,否则返回False
  683. '---缓存删除:
  684. 'Remove(key): 删除一个缓存,如果该缓存名称存在而且成功删除则返回True否则返回False
  685. 'RemoveAll(): 删除所有缓存,返回True或False
  686. 'DeleteAll(): 删除所有过期的缓存,返回TrueFalse(表示没有过期的缓存)
  687. '---缓存读取:
  688. '可以使用Item,ItemInfo,Keys,Items属性操作
  689. '---缓存检查:
  690. 'Exists(key): 检查一个缓存是否存在
  691. 'CheckExpires(key): 检查一个缓存是否已经过期,是返回True,否返回False
  692. '******************************/
  693.  
  694. Class XmlCache
  695.  
  696. Private xmlDoc'//内部xml对象
  697. Private isLoaded'//是否已经加载xml文档
  698.  
  699. Private xFile'//加载进来的xml文件(包括路径)
  700. Private xTitle
  701. Private xCreator
  702. Private xDateCreated
  703. Private xLastUpdate
  704. Private xDescription
  705. Private xEncoding
  706.  
  707. Private itemTemp'//保存item节点的xml摸板
  708.  
  709. Public IsAutoUpdate'//当在内存中修改xml时是否自动更新到该xml文件,默认是否False
  710.  
  711. Private Sub Class_Initialize()
  712. Set xmlDoc=getXmlObj()
  713. xTitle=""
  714. xCreator=""
  715. xDateCreated=Now()
  716. xLastUpdate=Now()
  717. xDescription=""
  718. xEncoding="GB2312"
  719. isLoaded=False
  720. IsAutoUpdate=False
  721. 'itemTemp=vbcrlf&vbcrlf
  722. itemTemp=itemTemp&" <Item>"&vbcrlf
  723. itemTemp=itemTemp&" <Key>{key}</Key>"&vbcrlf
  724. itemTemp=itemTemp&" <CreatedTime>{createdtime}</CreatedTime>"&vbcrlf
  725. itemTemp=itemTemp&" <Expires>{expires}</Expires>"&vbcrlf
  726. itemTemp=itemTemp&" <Value>"&vbcrlf
  727. itemTemp=itemTemp&" <![CDATA[{value}]]>"&vbcrlf
  728. itemTemp=itemTemp&" </Value>"&vbcrlf
  729. itemTemp=itemTemp&" </Item>"&vbcrlf
  730. End Sub
  731. Private Sub Class_Terminate()
  732. Set xmlDoc=Nothing
  733. End Sub
  734.  
  735. '返回整个xml文档内容,只读
  736. Public Property Get Xml
  737. Xml=xmlDoc.Xml
  738. End Property
  739.  
  740. '//Title节点
  741. Public Property Get Title
  742. On Error Resume Next
  743. If isLoaded Then
  744. xTitle=xmlDoc.selectSingleNode("/XmlCache/Title").Text
  745. End If
  746. If Err Then showErr "节点/XmlCache/Title不存在"
  747. Title=xTitle
  748. End Property
  749. Public Property Let Title(v)
  750. xTitle=v
  751. On Error Resume Next
  752. If isLoaded Then
  753. xmlDoc.selectSingleNode("/XmlCache/Title").Text=xTitle
  754. End If
  755. If Err Then showErr "节点/XmlCache/Title不存在"
  756. End Property
  757.  
  758. '//Creator节点
  759. Public Property Get Creator
  760. On Error Resume Next
  761. If isLoaded Then
  762. xCreator=xmlDoc.selectSingleNode("/XmlCache/Creator").Text
  763. End If
  764. If Err Then showErr "节点/XmlCache/Creator不存在"
  765. Creator=xCreator
  766. End Property
  767. Public Property Let Creator(v)
  768. xCreator=v
  769. On Error Resume Next
  770. If isLoaded Then
  771. xmlDoc.selectSingleNode("/XmlCache/Creator").Text=xCreator
  772. End If
  773. If Err Then showErr "节点/XmlCache/Creator不存在"
  774. End Property
  775.  
  776. '//DateCreated节点
  777. Public Property Get DateCreated
  778. On Error Resume Next
  779. If isLoaded Then
  780. xDateCreated=xmlDoc.selectSingleNode("/XmlCache/DateCreated").Text
  781. End If
  782. If Err Then showErr "节点/XmlCache/DateCreated不存在"
  783. DateCreated=xDateCreated
  784. End Property
  785. Public Property Let DateCreated(v)
  786. xDateCreatede=v
  787. On Error Resume Next
  788. If isLoaded Then
  789. xmlDoc.selectSingleNode("/XmlCache/DateCreated").Text=xDateCreated
  790. End If
  791. If Err Then showErr "节点/XmlCache/DateCreated不存在"
  792. End Property
  793.  
  794. '//LastUpdate节点
  795. Public Property Get LastUpdate
  796. On Error Resume Next
  797. If isLoaded Then
  798. xLastUpdate=xmlDoc.selectSingleNode("/XmlCache/LastUpdate").Text
  799. End If
  800. If Err Then showErr "节点/XmlCache/LastUpdate不存在"
  801. LastUpdate=xLastUpdate
  802. End Property
  803. Public Property Let LastUpdate(v)
  804. xLastUpdate=v
  805. On Error Resume Next
  806. If isLoaded Then
  807. xmlDoc.selectSingleNode("/XmlCache/LastUpdate").Text=xLastUpdate
  808. End If
  809. If Err Then showErr "节点/XmlCache/LastUpdate不存在"
  810. End Property
  811.  
  812. '//Description节点
  813. Public Property Get Description
  814. On Error Resume Next
  815. If isLoaded Then
  816. xDescription=xmlDoc.selectSingleNode("/XmlCache/Description").Text
  817. End If
  818. If Err Then showErr "节点/XmlCache/Description不存在"
  819. Description=xDescription
  820. End Property
  821. Public Property Let Description(v)
  822. xDescription=v
  823. On Error Resume Next
  824. If isLoaded Then
  825. xmlDoc.selectSingleNode("/XmlCache/Description").Text=xDescription
  826. End If
  827. If Err Then showErr "节点/XmlCache/Description不存在"
  828. End Property
  829.  
  830. '//Encoding
  831. Public Property Get Encoding
  832. On Error Resume Next
  833. If isLoaded Then
  834. xEncoding=xmlDoc.selectSingleNode("/XmlCache/Encoding").Text
  835. End If
  836. If Err Then showErr "节点/XmlCache/Encoding不存在"
  837. Encoding=xEncoding
  838. End Property
  839. Public Property Let Encoding(v)
  840. xEncoding=v
  841. On Error Resume Next
  842. If isLoaded Then
  843. xmlDoc.selectSingleNode("/XmlCache/Encoding").Text=xEncoding
  844. End If
  845. If Err Then showErr "节点/XmlCache/Encoding不存在"
  846. End Property
  847.  
  848. '//Item节点,设置或返回该缓存的值,可读写
  849. '//如果该值不存在则返回null值
  850. Public Default Property Get Item(key)
  851. Dim itemObj,k
  852. key=LCase(key)
  853. Set itemObj=xmlDoc.selectSingleNode("/XmlCache/Items/Item")
  854. For Each k In itemObj
  855. If k.childNodes.item(0).text=key Then
  856. Item=k.childNodes.item(3).text'缓存值
  857. Set itemObj=Nothing
  858. Exit Property
  859. End If
  860. Next
  861. Item=Null
  862. Set itemObj=Nothing
  863. End Property
  864. Public Property Let Item(key,v)
  865. Dim itemObj,k
  866. key=LCase(key)
  867. Set itemObj=xmlDoc.selectSingleNode("/XmlCache/Items/Item")
  868. On Error Resume Next
  869. For Each k In itemObj
  870. If k.childNodes.item(0).text=key Then
  871. k.childNodes.item(3).text=v'缓存值
  872. If Err Then
  873. showErr"缓存值不是有效的字符串"
  874. Set itemObj=Nothing
  875. Exit Property
  876. End If
  877. Set itemObj=Nothing
  878. Call Save()
  879. Exit Property
  880. End If
  881. Next
  882. Item=Null
  883. Set itemObj=Nothing
  884. Call Save()
  885. End Property
  886.  
  887. '//某个缓存的所有属性:名称,创建时间,过期时间截,值
  888. '//如果不存在则返回一个空值的数组
  889. Public Property Get ItemInfo(key)
  890. Dim itemObj,infoArr(3),i
  891. key=LCase(key)
  892. Set itemObj=xmlDoc.getElementsByTagName("Item")
  893. For i=0 To itemObj.length-1
  894. If itemObj.item(i).childNodes.item(0).text=key Then
  895. infoArr(0)=itemObj.item(i).childNodes.item(0).text'缓存名称
  896. infoArr(1)=itemObj.item(i).childNodes.item(1).text'创建时间
  897. infoArr(2)=itemObj.item(i).childNodes.item(2).text'过期时间截
  898. infoArr(3)=itemObj.item(i).childNodes.item(3).text'缓存值
  899. End If
  900. Next
  901. Set itemObj=Nothing
  902. ItemInfo=infoArr
  903. End Property
  904.  
  905. '//返回所有的缓存名称,组成一个数组,只读
  906. Public Property Get Keys()
  907. Dim keyObj,keyArr,i
  908. Set keyObj=xmlDoc.getElementsByTagName("Key")
  909. keyArr=Array()
  910. Redim keyArr(keyObj.length-1)
  911. For i=0 To keyObj.length-1
  912. keyArr(i)=keyObj.item(i).text
  913. Next
  914. Keys=keyArr
  915. Erase keyArr
  916. Set keyObj=Nothing
  917. End Property
  918.  
  919. '//返回所有缓存的数组,只读
  920. Public Property Get Items()
  921. Dim itemArr,itemInfoArr,itemObj,i
  922. Set itemObj=xmlDoc.getElementsByTagName("Item")
  923. itemArr=Array()
  924. ReDim itemArr(itemObj.length-1,3)
  925. For i=0 To itemObj.length-1
  926. itemArr(i,0)=itemObj.item(i).childNodes.item(0).text'缓存名称
  927. itemArr(i,1)=itemObj.item(i).childNodes.item(1).text'创建时间
  928. itemArr(i,2)=itemObj.item(i).childNodes.item(2).text'过期时间截
  929. itemArr(i,3)=itemObj.item(i).childNodes.item(3).text'缓存值
  930. Next
  931. Set itemObj=Nothing
  932. Items=itemArr
  933. Erase itemArr
  934. End Property
  935.  
  936. '//缓存个数,只读
  937. Public Property Get Count
  938. Count=xmlDoc.getElementsByTagName("Item").Length
  939. End Property
  940.  
  941. '/------------------------------------------------------
  942.  
  943. '//加载一份xml文档
  944. Public Sub Load(xmlFile)
  945. On Error Resume Next
  946. xmlDoc.Load(xmlFile)
  947. xFile=xmlFile
  948. If Err Then showErr "加载xml文档失败,Load(xmlFile),xmlFile:"&xmlFile
  949. isLoaded=True
  950. End Sub
  951.  
  952. '//创建一份xml文档
  953. Public Sub Create(xmlFile)
  954. Dim xmlText,newXmlDoc
  955. If xEncoding="" Then xEncoding="GB2312"
  956. xDateCreated=Now()
  957. xLastUpdate=Now()
  958. xmlText="<?xml version=""1.0"" encoding="""&Encoding&"""?>"&vbcrlf
  959. xmlText=xmlText&"<XmlCache>"&vbcrlf
  960. xmlText=xmlText&" <Title>"&Title&"</Title>"&vbcrlf
  961. xmlText=xmlText&" <Creator>"&Creator&"</Creator>"&vbcrlf
  962. xmlText=xmlText&" <DateCreated>"&CreatedTime&"</DateCreated>"&vbcrlf
  963. xmlText=xmlText&" <LastUpdate>"&LastUpdate&"</LastUpdate>"&vbcrlf
  964. xmlText=xmlText&" <Description>"&Description&"</Description>"&vbcrlf
  965. xmlText=xmlText&" <Encoding>"&Encoding&"</Encoding>"&vbcrlf
  966. xmlText=xmlText&" <Items>"&vbcrlf
  967. xmlText=xmlText&" </Items>"&vbcrlf
  968. xmlText=xmlText&"</XmlCache>"&vbcrlf
  969.  
  970. Set newXmlDoc=getXmlObj()
  971. On Error Resume Next
  972. newXmlDoc.LoadXml(xmlText)
  973. newXmlDoc.Save xmlFile
  974. If Err Then showErr "创建xml文档失败,Create(xmlFile),xmlFile:"&xmlFile
  975. Set newXmlDoc=Nothing
  976. End Sub
  977.  
  978. '//保存一份xml文档
  979. Public Sub Save()
  980. On Error Resume Next
  981. xmlDoc.Save xFile
  982. If Err Then showErr "保存xml文档失败,Save(),xmlFile:"&xmlFile
  983. End Sub
  984.  
  985. '//保存一份xml文档,文件名为xmlFile(全路径)
  986. Public Sub SaveAs(xmlFile)
  987. On Error Resume Next
  988. xmlDoc.Save xmlFile
  989. If Err Then showErr "保存xml文档失败,SaveAs(xmlFile),xmlFile:"&xmlFile
  990. End Sub
  991.  
  992. '//删除xml文档
  993. Public Sub DeleteFile()
  994. End Sub
  995.  
  996. '//检查缓存xml文档是否存在某个key,返回true或false
  997. '//检查一个缓存是否存在
  998. Public Function Exists(key)
  999. Dim itemObj,k
  1000. key=LCase(key)
  1001. Set itemObj=xmlDoc.selectNodes("/XmlCache/Items/Item/Key")
  1002. For Each k In itemObj
  1003. If k.text=key Then Exists=True:Exit Function
  1004. Next
  1005. Exits=Flase
  1006. End Function
  1007.  
  1008. '//添加一个缓存,失败返回false(比如:已经存在该key),成功返回true
  1009. Public Sub Add(key,value)
  1010. If key="" Then showErr"添加缓存失败,Add(key,value),key不能为空":Exit Sub
  1011. If Exists(key) Then showErr"添加缓存失败,Add(key,value),该key已经存在":Exit Sub
  1012.  
  1013. Dim itemsObj,itemObj,temp
  1014. key=LCase(key)
  1015. Set itemsObj=xmlDoc.documentElement.getElementsByTagName("Items")
  1016. If itemsObj.length>0 Then
  1017. temp=itemTemp
  1018. temp=Replace(temp,"{key}",key):temp=Replace(temp,"{value}",value)
  1019. temp=Replace(temp,"{createdtime}",Now()):temp=Replace(temp,"{expires}",60*20)
  1020. Set itemObj=getXmlObj()
  1021. itemObj.loadXml(temp)
  1022. Set itemObj=itemObj.documentElement.cloneNode(true)'//复制节点
  1023. itemsObj.item(0).appendChild itemObj
  1024. Call Save()
  1025. Set itemObj=Nothing
  1026. Else
  1027. showErr "添加缓存失败,Add(key,value),/XmlCache/Items节点不存在"
  1028. End If
  1029. Set ItemObj =Nothing
  1030. Set itemsObj=Nothing
  1031. End Sub
  1032.  
  1033. '//添加一个缓存,包括名称,值,过期时间
  1034. Public Sub AddFull(key,value,s)
  1035. If key="" Then showErr"添加缓存失败,AddFull(key,value,s),key不能为空":Exit Sub
  1036. If Not IsNumeric(s) Then showErr"添加缓存失败,AddFull(key,value,s),过期时间截s只能为数字":Exit Sub
  1037. If Exists(key) Then showErr"添加缓存失败,AddFull(key,value,s),该key已经存在":Exit Sub
  1038. Dim itemsObj,temp,xmlText,L
  1039. key=LCase(key)
  1040. Set itemsObj=xmlDoc.documentElement.getElementsByTagName("Items")
  1041. If itemsObj.length>0 Then
  1042. temp=itemTemp
  1043. temp=Replace(temp,"{key}",key):temp=Replace(temp,"{value}",value)
  1044. temp=Replace(temp,"{createdtime}",Now()):temp=Replace(temp,"{expires}",s)
  1045. Set itemObj=getXmlObj()
  1046. itemObj.loadXml(temp)
  1047. Set itemObj=itemObj.documentElement.cloneNode(true)'//复制节点
  1048. itemsObj.item(0).appendChild itemObj
  1049. Call Save()
  1050. Set itemObj=Nothing
  1051. Else
  1052. showErr "添加缓存失败,AddFull(key,value,s),/XmlCache/Items节点不存在"
  1053. End If
  1054. Set itemsObj=Nothing
  1055. End Sub
  1056.  
  1057. '//更新一个缓存值,如果缓存存在并更新成功返回True,否则返回False
  1058. Public Function Update(key,value)
  1059. Dim nodeItems,valueItems,i
  1060. key=LCase(key)
  1061. Set nodeItems=xmlDoc.getElementsByTagName("Key")
  1062. Set valueItems =xmlDoc.getElementsByTagName("Value")
  1063. On Error Resume Next
  1064. For i = 0 To nodeItems.length - 1
  1065. If nodeItems(i).text=key Then
  1066. valueItems(i).text=value
  1067. If Err Then
  1068. showErr "更新缓存失败,Update(key,value),Value节点不存在"
  1069. Update=False
  1070. Exit Function
  1071. End If
  1072. Update=True
  1073. Call xUpdate()
  1074. Exit Function
  1075. End If
  1076. Next
  1077. Set nodeItems=Nothing
  1078. Set valueItems=Nothing
  1079. Update=False
  1080. End Function
  1081.  
  1082. '//更新一个缓存的过期时间,如果缓存存在并更新成功返回True,否则返回False
  1083. Public Function UpdateExpires(key,s)
  1084. If Not IsNumeric(s) Then
  1085. showErr"更新缓存错误,UpdateTimeOut(key,s),过期时间截s只能为数字"
  1086. UpdateExpires=False
  1087. Exit Function
  1088. End If
  1089. Dim nodeItems,expiresItems,i
  1090. key=LCase(key)
  1091. Set nodeItems=xmlDoc.getElementsByTagName("Key")
  1092. Set expiresItems=xmlDoc.getElementsByTagName("Expires")
  1093. On Error Resume Next
  1094. For i = 0 To nodeItems.length - 1
  1095. If nodeItems(i).text=key Then
  1096. expiresItems(i).text=s
  1097. If Err Then
  1098. showErr "更新缓存失败,UpdateTimeOut(key,value),Expires节点不存在"
  1099. UpdateExpires=False
  1100. Exit Function
  1101. End If
  1102. UpdateExpires=True
  1103. Call xUpdate()
  1104. Exit Function
  1105. End If
  1106. Next
  1107. Set nodeItems=Nothing
  1108. Set expiresItems=Nothing
  1109. UpdateExpires=False
  1110. End Function
  1111.  
  1112. '//检查一个缓存是否已经过期,是返回True,否返回False
  1113. Public Function CheckExpires(key)
  1114. Dim keyObj,createdObj,expiresObj,i,s1,s2,s3
  1115. Set keyObj=xmlDoc.getElementsByTagName("Key")
  1116. Set createdObj=xmlDoc.getElementsByTagName("CreatedTime")
  1117. Set expiresObj=xmlDoc.getElementsByTagName("Expires")
  1118.  
  1119. For i=0 To keyObj.length-1
  1120. s1=keyObj.item(i).text
  1121. s2=createdObj.item(i).text
  1122. s3=expiresObj.item(i).text
  1123. If s1=key And IsDate(s2) And IsNumeric(s3) Then
  1124. If DateDiff("s",s1,Now())>CDbl(s2) Then
  1125. CheckExpires=True
  1126. Set keyObj=Nothing
  1127. Set createdObj=Nothing
  1128. Set expiresObj=Nothing
  1129. Exit Function
  1130. End If
  1131. End If
  1132. Next
  1133. Set keyObj=Nothing
  1134. Set createdObj=Nothing
  1135. Set expiresObj=Nothing
  1136. CheckExpires=False
  1137. End Function
  1138.  
  1139. '//Remove(key)删除一个缓存,如果该缓存名称存在而且成功删除则返回True否则返回False
  1140. Public Function Remove(key)
  1141. Dim keyObj,k
  1142. key=LCase(key)
  1143. Set keyObj=xmlDoc.getElementsByTagName("Key")
  1144. For Each k In keyObj
  1145. If k.text=key Then
  1146. k.parentNode.parentNode.removeChild(k.parentNode)
  1147. Remove=True
  1148. Set keyObj=Nothing
  1149. Exit Function
  1150. End If
  1151. Next
  1152. Remove=False
  1153. Set keyObj=Nothing
  1154. Call xUpdate()'//重新保存到文件
  1155. End Function
  1156.  
  1157. '//删除所有缓存,返回True或False
  1158. Public Function RemoveAll()
  1159. Dim itemsObj
  1160. Set itemsObj=xmlDoc.getElementsByTagName("Items")
  1161. If itemsObj.length=1 Then
  1162. itemsObj(0).text=""
  1163. RemoveAll=True
  1164. Else
  1165. RemoveAll=False
  1166. End If
  1167. Set itemsObj=Nothing
  1168. Call xUpdate()'//重新保存到文件
  1169. End Function
  1170.  
  1171. '//删除所有过期的缓存,返回True或False(表示没有过期的缓存)
  1172. Public Function DeleteAll()
  1173. Dim createdObj,expiresObj,isHave,i
  1174. isHave=False'//是否有过期的缓存
  1175. Set createdObj=xmlDoc.getElementsByTagName("CreatedTime")
  1176. Set expiresObj=xmlDoc.getElementsByTagName("Expires")
  1177.  
  1178. For i=0 To expiresObj.length-1
  1179. If IsDate(createdObj.item(i).text) And IsNumeric(expiresObj.item(i).text) Then
  1180. If DateDiff("s",createdObj.item(i).text,Now())>CDbl(expiresObj.item(i).text) Then
  1181. createdObj.item(i).parentNode.parentNode.removeChild(createdObj.item(i).parentNode)
  1182. isHave=True
  1183. End If
  1184. End If
  1185. Next
  1186. Set createdObj=Nothing
  1187. Set expiresObj=Nothing
  1188. DeleteAll=isHave
  1189. Call xUpdate()'//重新保存到文件
  1190. End Function
  1191.  
  1192. '//显示错误
  1193. Private Sub showErr(info)
  1194. If Err Then info=info&","&Err.Description
  1195. Response.Write info
  1196. Err.Clear
  1197. Response.End
  1198. End Sub
  1199.  
  1200. '//取得xml对象
  1201. Private Function getXmlObj()
  1202. On Error Resume Next
  1203. Set getXmlObj=Server.CreateObject("Microsoft.XMLDOM")
  1204. If Err Then showErr "创建xml对象失败"
  1205. End Function
  1206.  
  1207. '//更新一份xml文档
  1208. Private Sub xUpdate()
  1209. If IsAutoUpdate Then Call Save()
  1210. End Sub
  1211.  
  1212. '------------------------------------------------------/
  1213. End Class
  1214.  
  1215. %>
  1216. ==============================================================================
  1217. 动网先锋缓存类 提取 8.1 Dv_ClsMain.asp文件提取
  1218. 经过测试适用。。。。。。
  1219. 全文如下
  1220. <%
  1221. Dim dvbbs,txt
  1222. Set dvbbs=New Cls_Cache
  1223. Class Cls_Cache
  1224. Public Reloadtime,MaxCount,CacheName
  1225. Private LocalCacheName
  1226. Private Sub Class_Initialize()
  1227. Reloadtime=14400 ’默认缓存时间分钟
  1228. CacheName="dvbbs" ‘缓存总名
  1229. 'CacheName=LCase(CacheName)
  1230. End Sub
  1231. Public Property Let Name(ByVal vNewValue)
  1232. LocalCacheName = LCase(vNewValue)
  1233. End Property
  1234. Public Property Let Value(ByVal vNewValue)
  1235. If LocalCacheName<>"" Then
  1236. Application.Lock
  1237. Application(CacheName & "_" & LocalCacheName &"_-time")=Now()
  1238. Application(CacheName & "_" & LocalCacheName) = vNewValue
  1239. Application.unLock
  1240. End If
  1241. End Property
  1242. Public Property Get Value()
  1243. If LocalCacheName<>"" Then
  1244. Value=Application(CacheName & "_" & LocalCacheName)
  1245. End If
  1246. End Property
  1247. Public Function ObjIsEmpty()
  1248. ObjIsEmpty=True
  1249. If Not IsDate(Application(CacheName & "_" & LocalCacheName &"_-time")) Then Exit Function
  1250. If DateDiff("s",CDate(Application(CacheName & "_" & LocalCacheName &"_-time")),Now()) < (60*Reloadtime) Then ObjIsEmpty=False
  1251. End Function
  1252. Public Sub DelCahe(MyCaheName)
  1253. Application.Lock
  1254. Application.Contents.Remove(CacheName&"_"&MyCaheName & "_-time")
  1255. Application.Contents.Remove(CacheName&"_"&MyCaheName)
  1256. Application.unLock
  1257. End Sub
  1258. End Class
  1259. %>
  1260. 以上保存为一个文件
  1261. 如Cache.asp
  1262.  
  1263. 然后需要缓存数据的页面包含Cache.asp文件不用我说了吧
  1264. ‘’‘’‘’‘’‘调用开始
  1265. ’CacheName="dvbbs" ‘设立了缓存总名的可以不要这行 如果修改了这个 所有DVBBS 要修改 如dvbbs.Name就要改成新的对应的总名
  1266. dvbbs.Reloadtime=1 ’如果不按默认缓存时间才写 要不可以屏蔽
  1267. dvbbs.Name="01" ‘缓存子名必须
  1268. If vod.ObjIsEmpty() Then
  1269. txt=""
  1270. txt=“【这里是你要缓存的数据 可以是执行代码怎么写就看个人了】”
  1271. if txt = "" then txt = "暂无数据"&vbCrLf
  1272. txt=txt&"<!--上次更新"&now()&"下次更新将在"&dvbbs.Reloadtime&"分钟后-->"&vbCrLf
  1273. dvbbs.value=txt
  1274. Else
  1275. txt=dvbbs.value
  1276. End If
  1277. Response.Write txt ‘这里是输出显示可以修改适用’
  1278. ’‘’‘’‘’‘调用结束
  1279.  
  1280. ==========================================================================
  1281. 程序代码
  1282. <%
  1283. '***********************************************
  1284. '函数名:getcache
  1285. ' 用:将需要缓存的内容,置入缓存中,并读取出来,如果缓存中存在该内容,则直接从缓存读取!
  1286. '作 者: 静¢脉(hayden)
  1287. ' 间: 2007-12-21
  1288. '参 数:funsname ---- 需要缓存的内容
  1289. ' isreset ---- 是否更新[值:0(根据时间或判断缓存为空时自动更新)、1(主动更新)]
  1290. ' isarr ---- 所缓存的内容是否为一个数据[0为字符串,1为数组]
  1291. ' timeinfo ---- 缓存更新时间,单位为秒,当值为0时,则只在缓存为空时,才更新
  1292. '返回值:缓存名为"funsname”的内容
  1293. '***********************************************
  1294. Function getcache(funsname,isreset,isarr,timeinfo)
  1295. dim domain : domain = "myhhe.cn" '缓存域
  1296. Dim temp_getconfig
  1297. Dim re_getcache : re_getcache = False
  1298. Dim temp_isarray_type : temp_isarray_type = False
  1299. Dim Appfunsname : Appfunsname = Replace(Replace(Replace(funsname,"(",""),")",""),",",".")
  1300. If isarr = 1 Then temp_isarray_type = True
  1301. If isreset = 1 Then re_getcache = True
  1302. If isreset = 2 Then
  1303. execute("temp_getconfig="&funsname)
  1304. getcache = temp_getconfig
  1305. Exit Function
  1306. End If
  1307. If Application(domain&"_"&Appfunsname&"_time") = "" And timeinfo<>0 Then re_getcache = True
  1308. If Not re_getcache Then
  1309. If temp_isarray_type Then
  1310. If Not IsArray(Application(domain&"_"&Appfunsname)) Then re_getcache = True
  1311. Else
  1312. If Application(domain&"_"&Appfunsname) = "" Then re_getcache = True
  1313. End If
  1314. End If
  1315. If Not re_getcache And timeinfo<>0 Then
  1316. If Int(DateDiff("s",Application(domain&"_"&Appfunsname&"_time"),now()))>timeinfo Then re_getcache = True
  1317. End If
  1318. If re_getcache Then
  1319. execute("temp_getconfig="&funsname)
  1320. Application.Lock
  1321. Application(domain&"_"&Appfunsname) = temp_getconfig
  1322. Application(domain&"_"&Appfunsname&"_time") = Now()
  1323. Application.UnLock
  1324. Else
  1325. temp_getconfig=Application(domain&"_"&Appfunsname)
  1326. End If
  1327. getcache = temp_getconfig
  1328. End Function
  1329. %>
  1330.  
  1331. 调用示例:
  1332.  
  1333. 程序代码
  1334. <%
  1335. Function out_test1 '返回一个字符串的示例函数
  1336. out_test1="这里是一个字符串"
  1337. End Function
  1338.  
  1339. Function out_test2 '返回一个数组的示例函数
  1340. Dim temp_out_test2
  1341. temp_out_test2="这里.是.一个.数组"
  1342. out_test2=Split(temp_out_test2,".")
  1343. End Function
  1344.  
  1345. Dim i
  1346.  
  1347. '字符串缓存(将函数out_test1从缓存读取并输出)
  1348. Dim str2 : str2 = getcache("out_test1",0,0,180) '通过getcache函数读取缓存.刷新时间为180秒,(当out_test1缓存为空,会自动访问函数out_test1输出,并同时置入缓存~)
  1349. response.write str2
  1350.  
  1351. response.write "<BR><BR><BR>"
  1352.  
  1353. '数组缓存(将函数out_test2从缓存读取并输出)
  1354. Dim str1 : str1 = getcache("out_test2",0,1,180)  '同上(字符串缓存说明)
  1355. For i = 0 To UBound(str1)
  1356. response.write str1(i) & "<BR>"
  1357. Next
  1358. %>

  

ASP缓存类收集的更多相关文章

  1. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  2. HttpRuntime.Cache .Net自带的缓存类

    .Net自带的缓存有两个,一个是Asp.Net的缓存 HttpContext.Cache,一个是.Net应用程序级别的缓存,HttpRuntime.Cache. MSDN上有解释说: HttpCont ...

  3. 比较全面的一个PHP缓存类解析

    转自:http://www.blhere.com/1164.html 一.引论 PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站 ...

  4. 分享个 之前写好的 android 文件流缓存类,专门处理 ArrayList、bean。

    转载麻烦声明出处:http://www.cnblogs.com/linguanh/ 目录: 1,前序 2,作用 3,特点 4,代码 1,前序  在开发过程中,client 和 server 数据交流一 ...

  5. (实用篇)PHP缓存类完整实例

    本文完整描述了一个简洁实用的PHP缓存类,可用来检查缓存文件是否在设置更新时间之内.清除缓存文件.根据当前动态文件生成缓存文件名.连续创建目录.缓存文件输出静态等功能.对于采用PHP开发CMS系统来说 ...

  6. php简单缓存类

    <?phpclass Cache {    private $cache_path;//path for the cache    private $cache_expire;//seconds ...

  7. php简单数据缓存类

    公司手机触屏站 ,由于页面图片太多,所以需要做数据缓存,就随便写一个数据缓存类. 直接贴代码 <?php/**** fianl_m@foxmail.com* 缓存类* 把数据查询出,并序列化写入 ...

  8. iOS缓存类的设计

    使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...

  9. 一个不错的PHP文件页面缓存类

    在php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考. 页面缓存类 <?php    /*    * 缓存类    cac ...

随机推荐

  1. CDH ecosystem components

    1,Mahout ASF(Apache Software Foundation)开源项目,提供可扩展的`机器学习`--(ML,Machine Learning多领域交叉学科,涉及概率,统计,逼近,凸分 ...

  2. 细谈Linux和windows差异之图形化用户接口、命令行接口

    相信来看本博文的朋友,肯定是已经玩过linux好段时间了,才能深刻理解我此番话语. 这是在Windows下的命令行接口 这是windows下的用户接口 就是它,explorer.ext,可以去尝试.把 ...

  3. delphi 完全控制Excel 文件

    ( 一 ) 使用动态创建的方法 uses ComObj; 首先创建 Excel 对象Var   ExcelApp : Variant ;   ExcelApp := CreateOleObject ( ...

  4. hdoj 1872 稳定排序

    稳定排序 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. [OC Foundation框架 - 4] NSString的导出

      void exportString() { NSString *str = @"Hello, 坑爹"; NSString *path = @"/Users/hello ...

  6. iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer

    首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...

  7. web开发工具类

    1.日期工具类 import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { public sta ...

  8. nginx编译参数集合

    http://www.ttlsa.com/nginx/nginx-configure-descriptions/ 标题是不是很欠揍,个人认为确实值得一看,如果你不了解nginx,或者你刚学nginx, ...

  9. MySQL索引使用方法和性能优化

    在自己的一个项目中,数据比较多,搜索也很频繁,这里找到一个建立索引很不错的文章,推荐下. 关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的 ...

  10. 【转】使用junit4进行单元测试(高级篇)

    转自:http://blog.csdn.net/andycpp/article/details/1329218 通过前 2 篇文章,您一定对 JUnit 有了一个基本的了解,下面我们来探讨一下JUni ...