XmlDataSource.CacheDuration 屬性

定義

取得或設定資料來源控制項快取其所擷取之資料的時間長度 (以秒為單位)。Gets or sets the length of time, in seconds, that the data source control caches data it has retrieved.

public:
 virtual property int CacheDuration { int get(); void set(int value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.DataSourceCacheDurationConverter))]
public virtual int CacheDuration { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.DataSourceCacheDurationConverter))>]
member this.CacheDuration : int with get, set
Public Overridable Property CacheDuration As Integer

屬性值

Int32

XmlDataSource 控制項快取資料擷取作業結果的秒數。The number of seconds that the XmlDataSource control caches the results of a data retrieval operation. 預設值為 0。The default value is 0.

屬性

範例

下列程式碼範例示範如何在使用 XmlDataSource 控制項來顯示 XML 檔案中包含的資料時,啟用快取。The following code example demonstrates how to enable caching when using the XmlDataSource control to display data contained in an XML file. EnableCaching 屬性設定為 true ,且 CacheDuration 設定為數據源控制項快取資料的秒數時,即會啟用快取。Caching is enabled when the EnableCaching property is set to true and the CacheDuration is set to the number of seconds that the data is cached by the data source control.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml"
        enablecaching="True"
        cacheduration="60"
        cacheexpirationpolicy="Sliding" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml"
        enablecaching="True"
        cacheduration="60"
        cacheexpirationPolicy="Sliding" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>

程式碼範例中的 XML 檔案包含下列資料:The XML file in the code example has the following data:

<books>  
   <computerbooks>  
     <book title="Secrets of Silicon Valley" author="Sheryl Hunter"/>  
     <book title="Straight Talk About Computers" author="Dean Straight"/>  
     <book title="You Can Combat Computer Stress!" author="Marjorie Green"/>  
   </computerbooks>  
   <cookbooks>  
     <book title="Silicon Valley Gastronomic Treats" author="Innes del Castill"/>  
   </cookbooks>  
</books>  

備註

XmlDataSource當下列情況適用時,控制項會自動快取資料:The XmlDataSource control automatically caches data when the following applies:

  • EnableCaching 屬性會設定為 trueThe EnableCaching property is set to true.

  • CacheDuration屬性設定為大於0的值,表示快取在快取失效之前儲存資料的秒數。The CacheDuration property is set to a value greater than 0, which indicates the number of seconds that the cache stores data before the cache is invalidated.

對屬性的任何變更 Data 或包含 XML 資料的檔案內容,都會使快取失效。Any change to the Data property or the contents of the file that contains the XML data causes the cache to be invalidated.

根據預設, CacheDuration 屬性會設定為0,表示不限數量的快取,而且資料來源會快取資料,直到其相依的 XML 檔案變更為止。By default, the CacheDuration property is set to 0, which indicates an indefinite cache, and the data source will cache data until the XML file that it depends on is changed.

快取的行為是由持續時間和設定的組合所控管 CacheExpirationPolicyThe behavior of the cache is governed by a combination of the duration and the CacheExpirationPolicy setting. 如果 CacheExpirationPolicy 設定為 Absolute ,控制項會在 XmlDataSource 第一個資料抓取作業上快取資料、將它保存在記憶體中,以指定的時間量,並在經過一 CacheDuration 段時間後使其失效。If CacheExpirationPolicy is set to Absolute, the XmlDataSource control caches data on the first data retrieval operation, holds it in memory for the amount of time specified by CacheDuration, and invalidates it after the time has lapsed. 然後,會在下一次作業時重新整理快取。The cache is then refreshed upon the next operation. 如果 CacheExpirationPolicy 設定為 Sliding ,資料來源控制項會在第一個資料抓取作業上快取資料,但會在每次後續作業時重設保留快取的時間範圍。If CacheExpirationPolicy is set to Sliding, the data source control caches data on the first data retrieval operation, but resets the time window that it holds the cache for upon each subsequent operation. 快取只會在沒有任何活動的時間等於 CacheDuration 自上次資料抓取之後才會過期。The cache will only expire if there is no activity for a time equal to the CacheDuration since the last data retrieval.

適用於

另請參閱