zoukankan      html  css  js  c++  java
  • 使用文件依赖项缓存页输出

            当将页添加到输出缓存后,在超过了您在到期策略中指定的时间量之后,该页将被移除。有些时候,您要在到期之前从输出缓存中移除某个页或页的版本。若要执行该操作,可以使用 HttpResponse.AddFileDependency 方法使缓存页依赖于单个文件,或者使用 HttpResponse.AddFileDependencies 方法使缓存页依赖于 ArrayList 对象所表示的文件组。

            例如,如果您使用 XML 文件作为页的数据源,则创建一个 ArrayList,它将包含页使用的所有文件的名称。然后,可以调用 AddFileDependencies 使该页依赖于该 ArrayList。当任意 XML 文件发生更改时,该页将从输出缓存中移除,并根据下一请求生成一个新页。

    注意   不能从 Web 窗体用户控件使用这些方法。
    注意   通过调用 HttpResponse.RemoveOutputCacheItem 方法,可以从输出缓存中显式移除任何页。可以从 global.asax 文件,从已创建的自定义 ASP.NET 服务器控件或从页中执行该操作,具体取决于应用程序的需要。

    使缓存页输出依赖于一个文件

    1. 以声明方式或编程方式指定缓存页输出的设置。有关更多信息,请参见设置页缓存的到期时间设置页的可缓存性缓存页的多个版本
    2. 在页的代码声明块或代码隐藏文件中,创建 String 对象。该对象将在 AddFileDependency 方法调用中作为参数变量传递。
      [C#]
      static string filePath;
      [Visual Basic]
      Shared filePath As [String]
    3. Page_Load 方法或其他 ASP.NET 页生存期方法中,用 HttpServerUtility.MapPath 方法将第 2 步中所创建的字符串设置为依赖文件的绝对路径中。
      [C#]
      filePath = Server.MapPath("authors.xml");
      [Visual Basic]
      filePath = Server.MapPath("authors.xml")
    4. 在第 3 步所使用的同一页生存期方法中,访问 Response 属性语法以调用 AddFileDependency 方法,该操作将在 filename 参数中传递第 2 步所创建的 String 对象传递。
      [C#]
      Response.AddFileDependency(filePath);
      [Visual Basic]
      Response.AddFileDependency(filePath)

    使缓存页输出依赖于文件组

    1. 以声明方式或编程方式指定缓存页输出的设置。有关更多信息,请参见设置页缓存的到期时间设置页的可缓存性缓存页的多个版本
    2. 在页的代码声明块或代码隐藏文件中,创建包含依赖文件名称的 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"))
    3. 在一个页生存期方法中(如 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>

  • 相关阅读:
    利用JQuery一步步打造无缝滚动新闻
    asp.net操作oracle存储过程的小问题
    C# Tostring() 格式大全 [转]
    Oracle存储过程总结【转】
    JQuery中text()、html()和val()的区别
    JQuery的JSON数据格式
    HDU 1065 I Think I Need a Houseboat
    POJ1251 Jungle Roads
    HDU3177 Crixalis's Equipment
    HDU1042 N!
  • 原文地址:https://www.cnblogs.com/zsxfbj/p/144646.html
Copyright © 2011-2022 走看看