ASP.NET 缓存(五)--使用文件依赖项缓存页输出http://www.opent.cn/?action=show&id=192
2008-01-15 16:58
1016 查看
当将页添加到输出缓存后,在超过了您在到期策略中指定的时间量之后,该页将被移除。有些时候,您要在到期之前从输出缓存中移除某个页或页的版本。若要执行该操作,可以使用 HttpResponse.AddFileDependency 方法使缓存页依赖于单个文件,或者使用 HttpResponse.AddFileDependencies 方法使缓存页依赖于 ArrayList 对象所表示的文件组。
例如,如果您使用 XML 文件作为页的数据源,则创建一个 ArrayList,它将包含页使用的所有文件的名称。然后,可以调用 AddFileDependencies 使该页依赖于该 ArrayList。当任意 XML 文件发生更改时,该页将从输出缓存中移除,并根据下一请求生成一个新页。
注意 不能从 Web 窗体用户控件使用这些方法。注意 通过调用 HttpResponse.RemoveOutputCacheItem 方法,可以从输出缓存中显式移除任何页。可以从 global.asax 文件,从已创建的自定义 ASP.NET 服务器控件或从页中执行该操作,具体取决于应用程序的需要。
使缓存页输出依赖于一个文件
以声明方式或编程方式指定缓存页输出的设置。有关更多信息,请参见设置页缓存的到期时间、设置页的可缓存性和缓存页的多个版本。
在页的代码声明块或代码隐藏文件中,创建 String对象。该对象将在 AddFileDependency 方法调用中作为参数变量传递。
在 Page_Load 方法或其他 ASP.NET 页生存期方法中,用 HttpServerUtility.MapPath 方法将第 2 步中所创建的字符串设置为依赖文件的绝对路径中。
在第 3 步所使用的同一页生存期方法中,访问 Response 属性语法以调用 AddFileDependency 方法,该操作将在 filename 参数中传递第 2 步所创建的 String 对象传递。
使缓存页输出依赖于文件组
以声明方式或编程方式指定缓存页输出的设置。有关更多信息,请参见设置页缓存的到期时间、设置页的可缓存性和缓存页的多个版本。
在页的代码声明块或代码隐藏文件中,创建包含依赖文件名称的 ArrayList 对象。
在一个页生存期方法中(如 Page_Load),使用 Response 对象语法调用 AddFileDependencies 方法,该操作将在 filenames 参数中传递第 2 步所创建的 ArrayList 对象。
以下示例使用 AddFileDependency 方法使缓存页依赖于 XML 文件
例如,如果您使用 XML 文件作为页的数据源,则创建一个 ArrayList,它将包含页使用的所有文件的名称。然后,可以调用 AddFileDependencies 使该页依赖于该 ArrayList。当任意 XML 文件发生更改时,该页将从输出缓存中移除,并根据下一请求生成一个新页。
注意 不能从 Web 窗体用户控件使用这些方法。注意 通过调用 HttpResponse.RemoveOutputCacheItem 方法,可以从输出缓存中显式移除任何页。可以从 global.asax 文件,从已创建的自定义 ASP.NET 服务器控件或从页中执行该操作,具体取决于应用程序的需要。
使缓存页输出依赖于一个文件
以声明方式或编程方式指定缓存页输出的设置。有关更多信息,请参见设置页缓存的到期时间、设置页的可缓存性和缓存页的多个版本。
在页的代码声明块或代码隐藏文件中,创建 String对象。该对象将在 AddFileDependency 方法调用中作为参数变量传递。
[C#] static string filePath; [Visual Basic] Shared filePath As [String]
在 Page_Load 方法或其他 ASP.NET 页生存期方法中,用 HttpServerUtility.MapPath 方法将第 2 步中所创建的字符串设置为依赖文件的绝对路径中。
[C#] filePath = Server.MapPath("authors.xml"); [Visual Basic] filePath = Server.MapPath("authors.xml")
在第 3 步所使用的同一页生存期方法中,访问 Response 属性语法以调用 AddFileDependency 方法,该操作将在 filename 参数中传递第 2 步所创建的 String 对象传递。
[C#] Response.AddFileDependency(filePath); [Visual Basic] Response.AddFileDependency(filePath)
使缓存页输出依赖于文件组
以声明方式或编程方式指定缓存页输出的设置。有关更多信息,请参见设置页缓存的到期时间、设置页的可缓存性和缓存页的多个版本。
在页的代码声明块或代码隐藏文件中,创建包含依赖文件名称的 ArrayList 对象。
[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"))
在一个页生存期方法中(如 Page_Load),使用 Response 对象语法调用 AddFileDependencies 方法,该操作将在 filenames 参数中传递第 2 步所创建的 ArrayList 对象。
[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>
相关文章推荐
- ASP.NET 缓存(四)--使用缓存键依赖项缓存页输出http://www.opent.cn/?action=show&id=191
- ASP.NET 缓存(七)--根据参数缓存页的版本http://www.opent.cn/?action=show&id=194
- MFC 弹出窗口MessageBox的使用[转自http://www.bigasp.com.cn/blog/?action=show&id=51]
- VS .NET 2005中水晶报表的使用 http://www.tianyablog.com/blogger/post_show.asp?BlogID=116800&PostID=9731765
- 因ARP欺骗网站源文件被注入了代码http://www.cnbruce.com/blog/showlog.asp?cat_id=32&log_id=1191
- 解决ASP(图像)上传漏洞的方法http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=942
- 做了个非常简单的Flash验证码(附源代码) 转载于http://www.cordyblog.cn/?action=show&amp;id=68
- [转]ASP.NET 缓存(四)--使用缓存键依赖项缓存页输出
- 踏入社会(http://www.sysinfo.cn/index.php?action=show&id=90)
- 转:小结一下团队协作中的开源应用 转自:http://www.lingang.gd.cn/?action=show&id=329
- windows进程中的内存结构http://www.ieee.org.cn/dispbbs.asp?boardID=63&ID=39490
- 【转载】【权限控制】角色访问动态生成用户权限菜单树(转载: http://www.comprg.com.cn/post_show.asp?id=7873 )
- 女人:换一个爱情姿势好吗 转自 http://groups.tianya.cn/bulo/ShowArticle.asp?buloid=237&amp;ArticleID=413747&amp;idWriter=15308611&amp;key=59653
- ASP.NET下使用xml反序列化、缓存依赖实现个性化配置文件的实时生效
- http://acm.hnu.cn/online/?action=problem&type=show&id=12817&courseid=267 7.19hnu/数据结构/数学 xxs.code
- http://www.cppdev.cn/show.aspx?id=33&cid=6
- HTML在线编辑器的原理(转载整理)http://www.cnbruce.com/blog/showlog.asp?log_id=1168
- 明基成人礼:巅峰战将营 http://www.cnvn.com.cn/Article/ShowArticle.asp?ArticleID=3172
- ASP.NET中使用Server.Transfer()方法在页间传值 http://www.jaron.cn/chs_scripts/9/2004-09/101590.shtml
- http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10594&courseid=4