共用方式為


HttpResponse.AddCacheItemDependency(String) 方法

定義

讓快取回應的有效性取決於快取中的另一個項目。

public:
 void AddCacheItemDependency(System::String ^ cacheKey);
public void AddCacheItemDependency (string cacheKey);
member this.AddCacheItemDependency : string -> unit
Public Sub AddCacheItemDependency (cacheKey As String)

參數

cacheKey
String

項目的索引鍵,為快取回應所依存。

範例

下列範例是 ASP.NET 快取輸出的使用者控件。 控件的程式代碼會呼叫 AddCacheItemDependency 方法,其中含有儲存在 物件中 Cache 做為其參數的專案索引鍵。 如果專案不存在於快取中,則儲存在輸出快取中的控件回應會失效。 這表示在後續要求上,新的控件回應版本將會新增至輸出快取。

接下來,程式代碼會檢查與索引鍵相關聯的 bookData 專案是否儲存在物件中 Cache ,並顯示兩行文字中的其中一行相依於結果。 然後,程式代碼會設定名為 的控制項屬性,並呼叫自訂DataHelper類別的共用GetBookData方法,並使用方法填入 DataGridDataGriddgBooksDataSourceDataBind

    <%@ Control Language="c#" %>
    <%@ Outputcache duration="10" varybyparam="none" shared="True" %>
    <%@ Import Namespace = "Samples.AspNet.CS" %>
    <%@ Import Namespace = "System.Data" %>

<script runat="server">

    private void Page_Load(object sender, System.EventArgs e)
    {
    
        // Make user control invalid if the
        // cache item changes or expires.
        Response.AddCacheItemDependency("bookData");

        // Create a DataView object.
        DataView source = (DataView)Cache["bookData"];
    
        // Check if the view is stored in the cache
        // and generate the right label text
        // dependent upon what is returned.
        if (source == null)
        {
            source = DataHelper.GetBookData();
            lblCacheMsg.Text = "Data generated explicitly.";
        }
        else
        {
            lblCacheMsg.Text = "Data retrieved from application cache.";
        }
    
        dgBooks.DataSource = source;
        dgBooks.DataBind();
    
        lblOutputMessage.Text = DateTime.Now.ToString();
    }

</script>
    <table>
        <tbody>
            <tr>
                <th>
                    Books</th>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:DataGrid id="dgBooks" runat="server"></asp:DataGrid>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label id="lblCacheMsg" runat="server"></asp:Label>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    The control was created at: 
                </td>
                <td>
                    <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
                </td>
            </tr>
        </tbody>
    </table>
<%@ Control Language="vb" %>
<%@ Outputcache duration="10" varybyparam="none" shared="True" %>
<%@ Import Namespace="Samples.AspNet.VB" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">

    Private Sub Page_Load(sender As Object, e As System.EventArgs)
    
        ' Make user control invalid if the 
        ' cache item changes or expires.
        Response.AddCacheItemDependency("bookData")


        ' Create a DataView object.
        Dim source As DataView = Cache("bookData")
    
        ' Check if the view is stored in the cache
        ' and generate the right label text
        ' dependent upon what is returned.
        If source Is Nothing Then

           source = DataHelper.GetBookData()
           lblCacheMsg.Text = "Data generated explicitly."
        Else
           lblCacheMsg.Text = "Data retrieved from application cache."
        End If
    
        dgBooks.DataSource = source
        dgBooks.DataBind()
    
        lblOutputMessage.Text = DateTime.Now.ToString()
    End Sub

</script>

    <table>
        <tbody>
            <tr>
                <th>
                    Books</th>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:DataGrid id="dgBooks" runat="server"></asp:DataGrid>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label id="lblCacheMsg" runat="server"></asp:Label>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    The control was created at: 
                </td>
                <td>
                    <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
                </td>
            </tr>
        </tbody>
    </table>
     

備註

當對應至 cacheKey 參數的專案從快取中移除時,目前專案的快取回應無效。

適用於

另請參閱