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 메서드를 호출하여 라는 dgBooks컨트롤의 DataGrid 속성을 설정하고 DataSource 를 메서드로 DataBind 채웁니다DataGrid.

    <%@ 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 해당하는 항목이 캐시에서 제거되면 현재 항목의 캐시된 응답이 유효하지 않습니다.

적용 대상

추가 정보