ObjectDataSource.CacheExpirationPolicy ObjectDataSource.CacheExpirationPolicy ObjectDataSource.CacheExpirationPolicy ObjectDataSource.CacheExpirationPolicy Property

定义

获取或设置缓存的到期行为,该行为与持续时间组合在一起可以描述数据源控件所用缓存的行为。Gets or sets the cache expiration behavior that, when combined with the duration, describes the behavior of the cache that the data source control uses.

public:
 virtual property System::Web::UI::DataSourceCacheExpiry CacheExpirationPolicy { System::Web::UI::DataSourceCacheExpiry get(); void set(System::Web::UI::DataSourceCacheExpiry value); };
public virtual System.Web.UI.DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
member this.CacheExpirationPolicy : System.Web.UI.DataSourceCacheExpiry with get, set
Public Overridable Property CacheExpirationPolicy As DataSourceCacheExpiry

属性值

示例

本部分包含两个代码示例。This section contains two code examples. 第一个代码示例演示ObjectDataSource对象如何支持筛选和缓存。The first code example demonstrates how an ObjectDataSource object supports filtering and caching. 第二个代码示例演示如何实现一个返回DataSet对象的包装方法, 以便ObjectDataSource使用对象实现缓存和筛选。The second code example demonstrates how to implement a wrapper method that returns a DataSet object to enable caching and filtering with an ObjectDataSource object.

下面的代码示例演示ObjectDataSource控件如何支持筛选和缓存。The following code example demonstrates how an ObjectDataSource control supports filtering and caching. 若要启用筛选和缓存, 必须实现用于检索数据的方法, 该方法由SelectMethod属性标识, 以将数据DataSet作为对象返回。To enable filtering and caching, you must implement the method that retrieves data, which is identified by the SelectMethod property, to return the data as a DataSet object. 在此示例中, 启用EmployeeLogic了缓存DataSet, 因为对象将数据作为返回, EnableCaching属性设置为true, 并且设置了CacheDurationCacheExpirationPolicy属性。In this example, caching is enabled because the EmployeeLogic object returns data as a DataSet, the EnableCaching property is set to true, and the CacheDuration and CacheExpirationPolicy properties are set. ObjectDataSource SelectMethod属性返回的数据缓存30秒。The ObjectDataSource caches data that is returned by the SelectMethod property for 30 seconds.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ 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>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          typename="Samples.AspNet.CS.EmployeeLogic"
          selectmethod="GetAllEmployeesAsDataSet"
          enablecaching="True"
          cacheduration="30"
          cacheexpirationpolicy="Absolute" />

    </form>
  </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ 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>
    <title>ObjectDataSource - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          typename="Samples.AspNet.VB.EmployeeLogic"
          selectmethod="GetAllEmployeesAsDataSet"
          enablecaching="True"
          cacheduration="30"
          cacheexpirationpolicy="Absolute" />

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

下面的代码示例演示如何实现一个返回DataSet对象的包装方法, 以启用对ObjectDataSource控件的缓存和筛选。The following code example demonstrates how to implement a wrapper method that returns a DataSet object to enable caching and filtering with an ObjectDataSource control. EmployeeLogic类的基实现中GetAllEmployees , 方法返回ArrayListIn the base implementation of the EmployeeLogic class, the GetAllEmployees method returns an ArrayList. 添加一个名GetAllEmployeesAsDataSet为的包装方法, NorthwindEmployee该方法ObjectDataSource将作为返回一DataSet组数据, 而不是完全重构对象以与 Web 窗体页一起使用。Instead of refactoring the object completely to work with the ObjectDataSource on a Web Forms page, a wrapper method named GetAllEmployeesAsDataSet is added that returns a set of NorthwindEmployee data as a DataSet. 此代码示例是为ObjectDataSource类提供的更大示例的一部分。This code example is part of a larger example provided for the ObjectDataSource class.

//
// To support basic filtering, the employees cannot
// be returned as an array of objects, rather as a
// DataSet of the raw data values.
public static DataSet GetAllEmployeesAsDataSet () {
  ICollection employees = GetAllEmployees();

  DataSet ds = new DataSet("Table");

  // Create the schema of the DataTable.
  DataTable dt = new DataTable();
  DataColumn dc;
  dc = new DataColumn("FirstName", typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("LastName",  typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("Title",     typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("Courtesy",  typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("Supervisor",typeof(Int32));  dt.Columns.Add(dc);

  // Add rows to the DataTable.
  IEnumerator emplEnum = employees.GetEnumerator();
  DataRow row;
  NorthwindEmployee ne;
  while (emplEnum.MoveNext()) {
    ne = emplEnum.Current as NorthwindEmployee;
    row = dt.NewRow();
    row["FirstName"]  = ne.FirstName;
    row["LastName"]   = ne.LastName;
    row["Title"]      = ne.Title;
    row["Courtesy"]   = ne.Courtesy;
    row["Supervisor"] = ne.Supervisor;
    dt.Rows.Add(row);
  }
  // Add the complete DataTable to the DataSet.
  ds.Tables.Add(dt);

  return ds;
}
' To support basic filtering, the employees cannot
' be returned as an array of objects, rather as a
' DataSet of the raw data values.
Public Shared Function GetAllEmployeesAsDataSet() As DataSet
   Dim employees As ICollection = GetAllEmployees()

   Dim ds As New DataSet("Table")

   ' Create the schema of the DataTable.
   Dim dt As New DataTable()
   Dim dc As DataColumn
   dc = New DataColumn("FirstName", GetType(String))
   dt.Columns.Add(dc)
   dc = New DataColumn("LastName", GetType(String))
   dt.Columns.Add(dc)
   dc = New DataColumn("Title", GetType(String))
   dt.Columns.Add(dc)
   dc = New DataColumn("Courtesy", GetType(String))
   dt.Columns.Add(dc)
   dc = New DataColumn("Supervisor", GetType(Int32))
   dt.Columns.Add(dc)

   ' Add rows to the DataTable.
   Dim emplEnum As IEnumerator = employees.GetEnumerator()
   Dim row As DataRow
   Dim ne As NorthwindEmployee
   While emplEnum.MoveNext()
      ne = CType(emplEnum.Current, NorthwindEmployee)
      row = dt.NewRow()
      row("FirstName") = ne.FirstName
      row("LastName") = ne.LastName
      row("Title") = ne.Title
      row("Courtesy") = ne.Courtesy
      row("Supervisor") = ne.Supervisor
      dt.Rows.Add(row)
   End While
   ' Add the complete DataTable to the DataSet.
   ds.Tables.Add(dt)

   Return ds
End Function 'GetAllEmployeesAsDataSet

注解

ObjectDataSource控件支持数据缓存。The ObjectDataSource control supports data caching. 在缓存数据时, 对方法的Select调用将从缓存中检索数据, 而不是从ObjectDataSource处理的业务对象中检索数据。While data is cached, calls to the Select method retrieve data from the cache rather than from the business object that the ObjectDataSource works with. 缓存过期后, Select方法从业务对象中检索数据, 然后再次缓存数据。When the cache expires, the Select method retrieves data from the business object, and then caches the data again.

当属性设置为true , 并且CacheDuration属性设置为大于0的值时,控件将自动缓存数据,指示缓存在缓存条目之前存储数据的秒数ObjectDataSource EnableCaching被丢弃。The ObjectDataSource control automatically caches data when the EnableCaching property is set to true and 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 entry is discarded. 值0指示无限长的缓存时间。A value of 0 indicates an infinitely long cache period.

缓存按持续时间和CacheExpirationPolicy设置的组合进行管制。The cache is regulated by a combination of the duration and the CacheExpirationPolicy setting. ObjectDataSource Select CacheDuration如果将Absolute属性设置为值, 则会在第一次调用方法时缓存数据并将其保留在内存中, 最多可为属性指定的时间量。 CacheExpirationPolicyIf the CacheExpirationPolicy property is set to the Absolute value, the ObjectDataSource caches data on the first call to the Select method and holds it in memory for, at most, the amount of time that is specified by the CacheDuration property. 如果需要内存, 则数据可能在持续时间之前释放。The data might be released before the duration time, if the memory is needed. 然后, 在下一次调用Select方法时刷新缓存。The cache is then refreshed during the next call to the Select method. 如果将Sliding Select属性设置为值, 则数据源控件会在第一次调用方法时缓存数据, 但会在每次调用Select方法时重置其保存缓存的时间窗口。 CacheExpirationPolicyIf the CacheExpirationPolicy property is set to the Sliding value, the data source control caches data on the first call to the Select method, but resets the time window for which it holds the cache for each subsequent call to the Select method. 如果自上次调用CacheDuration Select方法后, 该属性的时间没有任何活动, 则缓存将过期。The cache expires if there is no activity for a time that is equal to the CacheDuration property since the last call to the Select method.

适用于

另请参阅