HttpResponse.AddCacheItemDependency(String) Metoda

Definicja

Sprawia, że ważność buforowanej odpowiedzi zależy od innego elementu w pamięci podręcznej.

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

Parametry

cacheKey
String

Klucz elementu, od którego zależy buforowana odpowiedź.

Przykłady

Poniższy przykład to ASP.NET kontrolka użytkownika, która jest zapisywana w pamięci podręcznej. Kod kontrolki wywołuje metodę AddCacheItemDependency z kluczem elementu przechowywanego Cache w obiekcie przekazanym jako jego parametr. Jeśli element nie istnieje w pamięci podręcznej, odpowiedź kontrolki, która została zapisana w wyjściowej pamięci podręcznej, zostanie unieważniona. Oznacza to, że w kolejnym żądaniu zostanie dodana nowa wersja odpowiedzi kontrolki do wyjściowej pamięci podręcznej.

Następnie kod sprawdza, czy element skojarzony z kluczem bookData jest przechowywany w Cache obiekcie i wyświetla jeden z dwóch wierszy tekstu zależnych od wyniku. Następnie kod ustawia DataSource właściwość kontrolki DataGrid o nazwie dgBooksz wywołaniem metody udostępnionej GetBookData klasy niestandardowej DataHelper i wypełnia DataGrid metodę DataBind metodą .

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

Uwagi

Gdy element odpowiadający cacheKey parametrowi zostanie usunięty z pamięci podręcznej, buforowana odpowiedź bieżącego elementu jest nieprawidłowa.

Dotyczy

Zobacz też