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.

适用于

另请参阅