HttpResponse.AddCacheItemDependency(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使缓存响应的有效性依赖于缓存中的其他项。
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 用户控件。 控件的代码使用作为其参数传递的对象中Cache存储的项的键调用 AddCacheItemDependency 方法。 如果缓存中不存在该项,则存储在输出缓存中的控件响应将失效。 这意味着,在后续请求中,控件响应的新版本将添加到输出缓存中。
接下来,代码检查与键关联的 bookData
项是否存储在 对象中 Cache
,并显示依赖于结果的两行文本中的一行。 然后,代码通过调用自定义类的DataGrid共享GetBookData
方法设置DataSource名为 dgBooks
的 控件的 属性,并使用 方法填充 DataGridDataBind 。DataHelper
<%@ 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
参数对应的项时,当前项的缓存响应无效。