2005/10/26 | 敝帚自珍:ASP状态封装类Cache、Cookie & Session
类别(Develop) | 评论(0) | 阅读(132) | 发表于 09:59
CacheState类,建议实例化时用名Cache

<%
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
%>


CookieState类,建议实例化时用名Cookie

<%
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类,建议实例化时用名Session

<%
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
%>
0

评论Comments

日志分类
首页[66]
Develop[14]
Plus[3]
Melody[7]
Essay[38]
Archive[4]