asp学习网: 首页 >> class:类 >> Cookie,Session,Application封装

Cookie,Session,Application封装

http://www.toyak.com/

再把其它的操作,比如Cookie,Session,Application封装
CookieState类:
<%
Class CookieState

Private CurrentKey

Public Default Property Get Contents(ByVal Value)
Contents = Values(Value)
End Property

Public Property Let Expires(ByVal Value)
Response.Cookies(CurrentKey).Expires = DateAdd("d", Value, Now)
End Property
Public Property Get Expires()
Expires = Request.Cookies(CurrentKey).Expires
End Property

Public Property Let Path(ByVal Value)
Response.Cookies(CurrentKey).Path = Value
End Property
Public Property Get Path()
Path = Request.Cookies(CurrentKey).Path
End Property

Public Property Let Domain(ByVal Value)
Response.Cookies(CurrentKey).Domain = Value
End Property
Public Property Get Domain()
Domain = Request.Cookies(CurrentKey).Domain
End Property

Public Sub Add(ByVal Key, ByVal Value, ByVal Options)
Response.Cookies(Key) = Value
CurrentKey = Key
If Not (IsNull(Options) Or IsEmpty(Options) Or Options = "") Then
If IsArray(Options) Then
Dim l : l = UBound(Options)
Expire = Options(0)
If l = 1 Then Path = Options(1)
If l = 2 Then Domain = Options(2)
Else
Expire = Options
End If
End If
End Sub

Public Sub Remove(ByVal Key)
CurrentKey = Key
Expires = -1000
End Sub

Public Sub RemoveAll()
Clear()
End Sub

Public Sub Clear()
Dim iCookie
For Each iCookie In Request.Cookies
Response.Cookies(iCookie).Expires = FormatDateTime(Now)
Next
End Sub

Public Function Values(ByVal Key)
Values = Request.Cookies(Key)
End Function

Private Sub Class_initialize()
End Sub
Private Sub Class_Terminate()
End Sub

End Class
%>

SessionState类:
<%
Class SessionState

Public Default Property Get Contents(ByVal Key)
Contents = Session(Key)
End Property

Public Property Let TimeOut(ByVal Value)
Session.TimeOut = Value
End Property

Public Property Get TimeOut()
TimeOut = Session.TimeOut
End Property

Public Sub Add(ByVal Key, ByVal Value)
Session(Key) = Value
End Sub

Public Sub Remove(ByVal Key)
Session.Contents.Remove(Key)
End Sub

Public Function Values(ByVal Key)
Values = Session(Key)
End Function

Public Sub Clear()
Session.Abandon()
End Sub

Public Sub RemoveAll()
Clear()
End Sub

Private Sub Class_initialize()
End Sub
Private Sub Class_Terminate()
End Sub

End Class
%>

Application类封装成CacheState类:
<%
Class CacheState

Private IExpires

Public Default Property Get Contents(ByVal Value)
Contents = Values(Value)
End Property

Public Property Let Expires(ByVal Value)
IExpires = DateAdd("d", Value, Now)
End Property
Public Property Get Expires()
Expires = IExpires
End Property

Public Sub Lock()
Application.Lock()
End Sub

Public Sub UnLock()
Application.UnLock()
End Sub

Public Sub Add(ByVal Key, ByVal Value, ByVal Expire)
Expires = Expire
Lock
Application(Key) = Value
Application(Key & "Expires") = Expires
UnLock
End Sub

Public Sub Remove(ByVal Key)
Lock
Application.Contents.Remove(Key)
Application.Contents.Remove(Key & "Expires")
UnLock
End Sub

Public Sub RemoveAll()
Clear()
End Sub

Public Sub Clear()
Application.Contents.RemoveAll()
End Sub

Public Function Values(ByVal Key)
Dim Expire : Expire = Application(Key & "Expires")
If IsNull(Expire) Or IsEmpty(Expire) Then
Values = ""
Else
If IsDate(Expire) And CDate(Expire) > Now Then
Values = Application(Key)
Else
Call Remove(Key)
Value = ""
End If
End If
End Function

Public Function Compare(ByVal Key1, ByVal Key2)
Dim Cache1 : Cache1 = Values(Key1)
Dim Cache2 : Cache2 = Values(Key2)
If TypeName(Cache1) <> TypeName(Cache2) Then
Compare = True
Else
If TypeName(Cache1)="Object" Then
Compare = (Cache1 Is Cache2)
Else
If TypeName(Cache1) = "Variant()" Then
Compare = (Join(Cache1, "^") = Join(Cache2, "^"))
Else
Compare = (Cache1 = Cache2)
End If
End If
End If
End Function

Private Sub Class_initialize()
End Sub
Private Sub Class_Terminate()
End Sub

End Class
%>

上面3个类,在实例化时可以用去掉State后的类名,比如
Dim Cookie : Set Cookie = New CookieState
Dim Session : Set Session = New SessionState
Dim Cache : Set Cache = New CacheState from:asp学习网/title:Cookie,Session,Application封装/ time:2006-4-26 22:10:11

本文主题Cookie,Session,Application封装

asp教程 ©2006-2007 aspxuexi.com | 关于站点 | 版权隐私 | 站内搜索
复制或者翻版 请于夜间进行