Export (0) Print
Expand All

XmlDataSource.CacheDuration Property

Gets or sets the length of time, in seconds, that the data source control caches data it has retrieved.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[TypeConverterAttribute(typeof(DataSourceCacheDurationConverter))]
public virtual int CacheDuration { get; set; }
<asp:XmlDataSource CacheDuration="Int32" />

Property Value

Type: System.Int32
The number of seconds that the XmlDataSource control caches the results of a data retrieval operation. The default value is 0.

The XmlDataSource control automatically caches data when the following applies:

  • The EnableCaching property is set to true.

  • 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.

Any change to the Data property or the contents of the file that contains the XML data causes the cache to be invalidated.

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.

The behavior of the cache is governed by a combination of the duration and the CacheExpirationPolicy setting. 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. 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. The cache will only expire if there is no activity for a time equal to the CacheDuration since the last data retrieval.

The following code example demonstrates how to enable caching when using the XmlDataSource control to display data contained in an XML file. 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>

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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft