当将页添加到输出缓存后,在超过了您在到期策略中指定的时间量之后,该页将被移除。有些时候,您要在到期之前从输出缓存中移除某个页或页的版本。若要执行该操作,可以使用 HttpResponse.AddFileDependency 方法使缓存页依赖于单个文件,或者使用 HttpResponse.AddFileDependencies 方法使缓存页依赖于 ArrayList 对象所表示的文件组。
例如,如果您使用 XML 文件作为页的数据源,则创建一个 ArrayList,它将包含页使用的所有文件的名称。然后,可以调用 AddFileDependencies 使该页依赖于该 ArrayList。当任意 XML 文件发生更改时,该页将从输出缓存中移除,并根据下一请求生成一个新页。
注意 不能从 Web 窗体用户控件使用这些方法。
注意 通过调用 HttpResponse.RemoveOutputCacheItem 方法,可以从输出缓存中显式移除任何页。可以从 global.asax 文件,从已创建的自定义 ASP.NET 服务器控件或从页中执行该操作,具体取决于应用程序的需要。
使缓存页输出依赖于一个文件
[C#] static string filePath; [Visual Basic] Shared filePath As [String]
[C#] filePath = Server.MapPath("authors.xml"); [Visual Basic] filePath = Server.MapPath("authors.xml")
[C#] Response.AddFileDependency(filePath); [Visual Basic] Response.AddFileDependency(filePath)
使缓存页输出依赖于文件组
[C#] ArrayList depAL = new ArrayList(); depAL.Add(Server.MapPath("authors.xml")); depAL.Add(Server.MapPath("products.xml")); [Visual Basic] Dim depAL As New ArrayList() depAL.Add(Server.MapPath("authors.xml")) depAL.Add(Server.MapPath("products.xml"))
[C#] Response.AddFileDependencies(depAL); [Visual Basic] Response.AddFileDependencies(depAL)
以下示例使用 AddFileDependency 方法使缓存页依赖于 XML 文件 authors.xml
。一旦请求了该页,该页将在输出缓存中存储 100 秒。不过,如果在该时间到期之前,对该 XML 文件进行了更改,则该页输出将从缓存中移除。下次对该页进行请求时,输出缓存中将存储该页的新版本。若要对其进行测试,请在请求该页后,将其他项手动添加到 XML 文件中,然后从浏览器刷新该页。
[C#] <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%@ OutputCache duration="100" varybyparam="false" %> <html> <script language="C#" runat="server"> static String filePath; void Page_Load(Object Src, EventArgs E ) { filePath = Server.MapPath("Authors.xml"); Response.AddFileDependency(filePath); LoadData(); TimeMsg.Text = DateTime.Now.ToString("G"); } void LoadData() { // Read the data from the XML source. DataSet ds = new DataSet(); FileStream fs = new FileStream(Server.MapPath("authors.xml"), FileMode.Open,FileAccess.Read); StreamReader reader = new StreamReader(fs); ds.ReadXml(reader); fs.Close(); // Create a DataView object to bind to // the DataGrid on the page. DataView Source = new DataView(ds.Tables[0]); // Bind the Source to the DataGrid on the page. MyDataGrid.DataSource = Source; MyDataGrid.DataBind(); } </script> <body> <form runat="server"> <h3><font face="Verdana">File Dependencies</font></h3> <ASP:DataGrid id="MyDataGrid" runat="server" Width="300" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" /> <p> <i>Last generated on:</i> <asp:label id="TimeMsg" runat="server" /> </form> </body> </html> [Visual Basic] <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%@ OutputCache duration="100" varybyparam="false" %> <html> <script language="VB" runat="server"> ' Create a string to pass to AddFileDependency. Shared filePath As [String] Sub Page_Load(Src As [Object], E As EventArgs) ' Assign the string the absolute path to the ' dependent file, then pass it to AddFileDependency. filePath = Server.MapPath("Authors.xml") Response.AddFileDependency(filePath) ' Call this function to display XML data in the page. LoadData() TimeMsg.Text = DateTime.Now.ToString("G") End Sub 'Page_Load Sub LoadData() ' Read the data from the XML source. Dim ds As New DataSet() Dim fs As New FileStream(Server.MapPath("authors.xml"), FileMode.Open, FileAccess.Read) Dim reader As New StreamReader(fs) ds.ReadXml(reader) fs.Close() ' Create a DataView object to bind to ' the DataGrid on the page. Dim [Source] As New DataView(ds.Tables(0)) ' Bind the Source to the DataGrid on the page. MyDataGrid.DataSource = [Source] MyDataGrid.DataBind() End Sub 'LoadData </script> <body> <form runat="server"> <h3><font face="Verdana">File Dependencies</font></h3> <ASP:DataGrid id="MyDataGrid" runat="server" Width="300" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" /> <p> <i>Last generated on:</i> <asp:label id="TimeMsg" runat="server" /> </form> </body> </html>
authors.xml
文件的内容如下所示。
<NewDataSet> <Table> <au_id>172-32-1176</au_id> <au_lname>White</au_lname> <au_fname>Johnson</au_fname> </Table> <Table> <au_id>213-46-8915</au_id> <au_lname>Green</au_lname> <au_fname>Marjorie</au_fname> </Table> </NewDataSet>