HttpResponse.AddCacheItemDependency(String) 方法

定義

讓快取回應的有效性取決於快取中的另一個項目。Makes the validity of a cached response dependent on another item in the cache.

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

參數

cacheKey
String

項目的索引鍵,為快取回應所依存。The key of the item that the cached response is dependent upon.

範例

下列範例是已快取輸出的 ASP.NET 使用者控制項。The following example is an ASP.NET user control that is output cached. 控制項的程式碼會呼叫AddCacheItemDependency方法,並將儲存Cache在物件中的專案索引鍵當做參數傳遞。The code for the control calls the AddCacheItemDependency method with the key of an item stored in the Cache object passed as its parameter. 如果專案不存在於快取中,則儲存在輸出快取中的控制項回應會無效。If the item does not exist in the cache, the control's response that was stored in the output cache is invalidated. 這表示在後續要求中,會將控制項回應的新版本新增至輸出快取。This means that on the subsequent request, a new version of the control's response will be added to the output cache.

接下來,程式碼會檢查與bookData索引鍵相關聯的專案是否儲存Cache在物件中,並根據結果顯示兩行文字的其中一個。Next, the code checks whether an item associated with a bookData key is stored in the Cache object, and displays one of two lines of text dependent upon the result. 然後,程式碼會使用DataSource自訂DataHelper類別DataGrid的共用GetBookData方法來設定dgBooks名為的控制項的屬性,並DataGrid使用DataBind方法填入。Then, the code sets the DataSource property of a DataGrid control, which is named dgBooks, with a call to a custom DataHelper class' shared GetBookData method, and populates the DataGrid with the DataBind method.

    <%@ 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至參數的專案時,目前專案的快取回應會無效。When the item corresponding to the cacheKey parameter is removed from the cache, the cached response of the current item is invalid.

適用於

另請參閱