Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

HttpResponse.AddCacheItemDependency Method

Makes the validity of a cached response dependent on another item in the cache.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

public void AddCacheItemDependency (
	string cacheKey
)
public void AddCacheItemDependency (
	String cacheKey
)
public function AddCacheItemDependency (
	cacheKey : String
)

Parameters

cacheKey

The key of the item that the cached response is dependent upon.

When the item corresponding to cacheKey is removed from the cache, the cached response of the current item is invalid.

TopicLocation
How to: Cache Versions of a Page Using HTTP HeadersBuilding ASP .NET Web Applications
How to: Set the Cacheability of an ASP.NET Page DeclarativelyBuilding ASP .NET Web Applications
How to: Set a Page's Cacheability ProgrammaticallyBuilding ASP .NET Web Applications
How to: Cache Page Output with File DependenciesBuilding ASP .NET Web Applications
How to: Cache Versions of a Page Using Requesting BrowserBuilding ASP .NET Web Applications
How to: Set Expiration Values for ASP.NET Page CachingBuilding ASP .NET Web Applications
How to: Cache Page Output with Cache Key DependenciesBuilding ASP .NET Web Applications
How to: Retrieve Values of Cached ItemsBuilding ASP .NET Web Applications
How to: Cache Versions of a Page Using HTTP HeadersBuilding ASP .NET Web Applications
How to: Set the Cacheability of an ASP.NET Page DeclarativelyBuilding ASP .NET Web Applications
How to: Set a Page's Cacheability ProgrammaticallyBuilding ASP .NET Web Applications
How to: Cache Page Output with File DependenciesBuilding ASP .NET Web Applications
How to: Cache Versions of a Page Using Requesting BrowserBuilding ASP .NET Web Applications
How to: Set Expiration Values for ASP.NET Page CachingBuilding ASP .NET Web Applications
How to: Cache Page Output with Cache Key DependenciesBuilding ASP .NET Web Applications
How to: Retrieve Values of Cached ItemsBuilding ASP .NET Web Applications

The following code example is an ASP.NET user control that is output cached. 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.

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. Then, the code sets the DataSource property of a DataGrid control, named dgBooks, with a call to a custom DataHelper class's 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="VJ#" %>
    <%@ Outputcache duration="10" varybyparam="none" shared="True" %>
    <%@ Import Namespace = "Samples.AspNet.JSL" %>
    <%@ 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.
        get_Response().AddCacheItemDependency("bookData");

        // Create a DataView object.
        DataView source = (System.Data.DataView)(get_Cache()
            .get_Item("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.set_Text("Data generated explicitly.");
        }
        else {
            lblCacheMsg.set_Text("Data retrieved from application cache.");
        }

        dgBooks.set_DataSource(source);
        dgBooks.DataBind();

        lblOutputMessage.set_Text(DateTime.get_Now().ToString());
    } //Page_Load

</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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft