Makes the validity of a cached response dependent on another item in the cache.
Assembly: System.Web (in System.Web.dll)
When the item corresponding to the cacheKey parameter is removed from the cache, the cached response of the current item is invalid.
The following example is an ASP.NET user control that is output cached. The code for the control calls the 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, which is named dgBooks, with a call to a custom DataHelper class's shared GetBookData method, and populates the DataGrid with the DataBind method.
<%@ 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>
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.