HttpResponse.AddCacheItemDependency メソッド
アセンブリ: System.Web (system.web.dll 内)


cacheKey に対応するアイテムがキャッシュから削除されると、現在のアイテムのキャッシュされた応答は無効になります。

次のコード例は、出力キャッシュされた ASP.NET ユーザー コントロールです。このコントロールのコードは、AddCacheItemDependency メソッドを呼び出し、Cache オブジェクトに格納されているアイテムのキーをパラメータとして渡します。該当するアイテムがキャッシュにない場合、出力キャッシュに格納されていたコントロールの応答は無効になります。つまり、後続の要求では、そのコントロールの応答の新しいバージョンが出力キャッシュに追加されます。
次に、bookData キーに関連付けられているアイテムが Cache オブジェクトに格納されているかどうかを確認し、その結果に応じて 1 行または 2 行のテキストを表示します。続いて、DataGrid コントロールの DataSource プロパティである dgBooks を、カスタム DataHelper クラスの共有 GetBookData メソッドを呼び出して設定し、DataGrid を DataBind メソッドを使用して設定します。
<%@ 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>
<%@ 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- HttpResponse.AddCacheItemDependency メソッドのページへのリンク