為了讓資料存取的速度加快, 我們可以設定CACHE把資料站存在伺服器的記憶體.\
如下:

<%@ Page Language=”VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<script runat=”server”>

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = Now
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsNothing(Cache(“data”)) Then
            Label2.Text = Cache(“data”).ToString()
        Else
            Label2.Text = “不存在…”
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Cache.Insert(“data”, TextBox1.Text & ” – ” & Now, Nothing, Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration)
       
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Cache.Insert(“data”, TextBox1.Text & ” – ” & Now, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10))
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Cache.Insert(“data”, TextBox1.Text & ” – ” & Now, New CacheDependency(Server.MapPath(“~/Mod05/Books.xml”)))
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Cache.Insert(“data”, TextBox1.Text & ” – ” & Now, Nothing, Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, New CacheItemRemovedCallback(AddressOf RemovedCallback))
    End Sub
   
    ‘當cache 被移除的時候, 寫一個TXT檔在C:\
    Public Sub RemovedCallback(ByVal k As String, ByVal v As Object, ByVal r As CacheItemRemovedReason)
        Dim sw As New IO.StreamWriter(“c:\2544\log.txt”, True)
        sw.WriteLine(“快取物件 ” & k & ” 已經移除, 原因是 : ” & r.ToString)
        sw.Close()
    End Sub

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        Now =
        <asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label><br />
        <br />
        Data :
        <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
        <asp:Button ID=”Button1″ runat=”server” Text=”絕對過期” OnClick=”Button1_Click” />
        <asp:Button ID=”Button3″ runat=”server” OnClick=”Button3_Click” Text=”相對過期” />
        <asp:Button ID=”Button4″ runat=”server” OnClick=”Button4_Click” Text=”相依檔案” />
        <asp:Button ID=”Button5″ runat=”server” OnClick=”Button5_Click” Text=”快取釋放回呼” /><br />
        <br />
        <br />
        <br />
        <br />
        <asp:Button ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”取得快取資料” />
        <asp:Label ID=”Label2″ runat=”server” Text=”Label”></asp:Label></div>
    </form>
</body>
</html>

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required