程序代碼
<%
'by xilou,www.chinacms.org,20090115
'最后更新:20090115
'修改記錄:無
CONST CACHEPREFIX = "CACHE_" '緩存前綴,不能為空
'說明:
' 1,緩存的格式為application(CACHEPREFIX & key) = array("緩存key","緩存時(shí)間","緩存內(nèi)容","緩存說明","到期時(shí)間")
' 2,緩存key不區(qū)分大小寫
'添加緩存,不檢查緩存是否存在,如果存在則相當(dāng)于更新緩存
'varAry : 參數(shù),格式為:array("緩存key","緩存時(shí)間","緩存內(nèi)容","緩存說明")
' 緩存key :application()格式相同
' 緩存時(shí)間:單位秒,可以為負(fù)數(shù),表示立即過期,可以為空,空或不是數(shù)字則默認(rèn)為20分鐘過期
' 緩存內(nèi)容:緩存數(shù)據(jù),不支持對對象的緩存
' 緩存說明:緩存描述說明
Function AddCache(varAry)
Dim c,ary(4)
If Not IsArray(varAry) Then
Response.Write "Error:AddCache(varAry)參數(shù)錯誤,參數(shù)不是數(shù)組"
Response.End()
End If
If UBound(varAry) <> 3 Then
Response.Write "Error:AddCache(varAry)參數(shù)錯誤,數(shù)組長度錯誤"
Response.End()
End If
If varAry(0) = "" Then
Response.Write "Error:AddCache(varAry)錯誤,key不能為空"
Response.End()
End If
If varAry(1) = "" or Not IsNumeric(varAry(1)) Then varAry(1) = 1200
Application.Lock()
Application(CACHEPREFIX & varAry(0)) = array(varAry(0),varAry(1),varAry(2),varAry(3),DateAdd("s",varAry(1),Now()))
Application.UnLock()
End Function
'檢查某個(gè)緩存是否存在,存在返回True否則返回False
'key : 緩存key
Function CheckCache(key)
Dim k
For Each k In Application.Contents
If LCase(k) = LCase(CACHEPREFIX & key) Then CheckCache = True : Exit Function
Next
CheckCache = False
End Function
'獲取緩存
'返回?cái)?shù)組,格式為:array("緩存key","緩存時(shí)間","緩存內(nèi)容","緩存說明","到期時(shí)間",是否過期True|False)
'如果不存在則出錯,所以獲取之前先用CheckCache(key)檢查
Function GetCache(key)
Dim app,isExp
app = Application(CACHEPREFIX & key)
isExp = False
If DateDiff("s",Now(),app(4)) <= 0 Then isExp = True
GetCache = Array(app(0),app(1),app(2),app(3),app(4),isExp)
End Function
'清除緩存
Function RemoveCache(key)
Application.Lock()
Application.Contents.Remove(CACHEPREFIX & key)
Application.UnLock()
End Function
'更新緩存,如果緩存不存在則出錯,所以更新之前先用CheckCache(key)檢查
'varAry : 參數(shù),格式為:array("緩存key","緩存時(shí)間","緩存內(nèi)容","緩存說明")
' 緩存key :application()格式相同
' 緩存時(shí)間:單位秒,可以為負(fù)數(shù),表示立即過期,可以為空,空或不是數(shù)字則默認(rèn)為20分鐘過期
' 緩存內(nèi)容:緩存數(shù)據(jù),不支持對對象的緩存
' 緩存說明:緩存描述說明
'注意 : 如果不更新varAry某個(gè)值則設(shè)置該值為null即可,
' 如UpdateCache(array("key",null,"內(nèi)容",null)),就是不更新過期時(shí)間和說明
Function UpdateCache(varAry)
Dim app
app = GetCache(varAry(0))
If Not IsNull(varAry(1)) Then app(1) = varAry(1)
If Not IsNull(varAry(2)) Then app(2) = varAry(2)
If Not IsNull(varAry(3)) Then app(3) = varAry(3)
If app(1) = "" or Not IsNumeric(app(1)) Then app(1) = 1200
Application.Lock()
Application(CACHEPREFIX & app(0)) = array(app(0),app(1),app(2),app(3),DateAdd("s",app(1),Now()))
Application.UnLock()
End Function
'www.companysz.com
'打印cache,做調(diào)試用
Function PrintCache(key)
Dim app,i,t
If CheckCache(key) Then
app = GetCache(key)
Response.Write "<pre>{"&chr(10)
Response.Write chr(32) & "緩存名稱" & chr(32) & ":" & chr(32) & CACHEPREFIX & app(0) & chr(10)
Response.Write chr(32) & "緩存key " & chr(32) & ":" & chr(32) & app(0) & chr(10)
Response.Write chr(32) & "緩存時(shí)間" & chr(32) & ":" & chr(32) & app(1) & chr(10)
Response.Write chr(32) & "到期時(shí)間" & chr(32) & ":" & chr(32) & app(4) & chr(10)
Response.Write chr(32) & "是否到期" & chr(32) & ":" & chr(32) & app(5) & chr(10)
Response.Write chr(32) & "緩存說明" & chr(32) & ":" & chr(32) & app(3) & chr(10)
'內(nèi)容
Response.Write chr(32) & "緩存內(nèi)容" & chr(32) & ":" & chr(32)
t = VarType(app(2))
If InStr(",0,1,2,3,4,5,6,7,8,11,",","&t&",") > 0 Then
Response.Write app(2)
Else
Response.Write TypeName(app(2))
End If
Response.Write chr(10)
Response.Write "}</pre>"&chr(10)
Else
Response.Write "不存在該緩存"
End If
End Function
'-----------demo
Sub br(str)
Response.Write str & "<br />" & vbcrlf
End Sub
'RemoveCache "xilou"
'AddCache Array("xilou","",array("數(shù)據(jù)內(nèi)容"),"緩存說明")
br CheckCache("xilou")
PrintCache "xilou"
Dim app
If CheckCache("xilou") Then
app = GetCache("xilou") '獲取
UpdateCache array(app(0),null,"testsfsfsf",null)'更新
Else
AddCache array("xilou","","內(nèi)容","說明")
End If
'Dim k
'For Each k In Application.Contents
'br k
'Next
%>
新聞熱點(diǎn)
疑難解答
圖片精選