SqlDataSource.CacheExpirationPolicy SqlDataSource.CacheExpirationPolicy SqlDataSource.CacheExpirationPolicy SqlDataSource.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

属性值

示例

下面的代码示例演示了如何在 Microsoft SQL Server 中从 Northwind 数据库检索数据, 并在GridView控件中显示数据缓存。The following code example demonstrates how to retrieve data from the Northwind database in Microsoft SQL Server and display it in a GridView control, with data caching enabled. 由于属性设置Sliding为值, 因此即使CacheDuration值为20秒, 用户也不会看到新数据。 CacheExpirationPolicyBecause the CacheExpirationPolicy property is set to the Sliding value, users will not see fresh data even though the CacheDuration value is 20 seconds. 仅当页面上没有任何活动超过20秒后, 才会刷新缓存的数据。The cached data is refreshed only if there is no activity on the page for more than 20 seconds since the last page load.

<!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:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                DataSourceMode="DataSet"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                EnableCaching="True"
                CacheDuration="20"
                CacheExpirationPolicy="Sliding"
                SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
            </asp:SqlDataSource>

            <asp:GridView
                id="GridView1"
                runat="server"
                AutoGenerateColumns="False"
                DataSourceID="SqlDataSource1">
                <columns>
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                    <asp:BoundField HeaderText="Title" DataField="Title" />
                </columns>
            </asp:GridView>

        </form>
    </body>
</html>
<!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:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                DataSourceMode="DataSet"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                EnableCaching="True"
                CacheDuration="20"
                CacheExpirationPolicy="Sliding"
                SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
            </asp:SqlDataSource>

            <asp:GridView
                id="GridView1"
                runat="server"
                AutoGenerateColumns="False"
                DataSourceID="SqlDataSource1">
                <columns>
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                    <asp:BoundField HeaderText="Title" DataField="Title" />
                </columns>
            </asp:GridView>

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

注解

SqlDataSource控件支持数据缓存。The SqlDataSource control supports data caching. 在缓存数据时, Select方法从缓存而不是基础数据库中检索数据。While data is cached, the Select method retrieves data from the cache rather than from the underlying database. 缓存过期后, 该方法Select将从基础数据库中检索数据, 然后再次缓存数据。When the cache expires, the Select method retrieves data from the underlying database, and then caches the data again.

缓存的行为由CacheDurationCacheExpirationPolicy设置的组合决定。The behavior of the cache is determined by a combination of the CacheDuration and CacheExpirationPolicy settings. 如果将Absolute SqlDataSource属性设置为值, 则将在第一个数据检索操作上缓存数据, 并在内存中将其保存在CacheDuration属性指定的时间量内, 然后在此时间后将其丢弃。 CacheExpirationPolicy失效.If the CacheExpirationPolicy property is set to the Absolute value, the SqlDataSource caches data on the first data retrieval operation, holds it in memory for the amount of time that is specified by the CacheDuration property, and then discards it after the time has lapsed. 然后, 在下一操作期间刷新缓存。The cache is then refreshed during the next operation. 如果将Sliding属性设置为值, 则数据源控件会在第一次数据检索操作中缓存数据, 但会重置其保存每个后续操作的缓存的时间窗口。 CacheExpirationPolicyIf the CacheExpirationPolicy property is set to the Sliding value, the data source control caches data on the first data retrieval operation, but resets the time window that it holds the cache for each subsequent operation. 如果自上一CacheDuration Select次操作后, 如果时间与值相等, 则缓存过期。The cache expires, if there is no activity for a time that is equal to the CacheDuration value since the last Select operation.

SqlDataSource 只有DataSet在模式下, 控件才能缓存数据。The SqlDataSource control can cache data only when in the DataSet mode. 如果NotSupportedException Select 控件设置DataReader为值, 并且还启用了缓存, 则方法会引发异常。 SqlDataSourceA NotSupportedException exception is thrown by the Select method, if the SqlDataSource control is set to the DataReader value and caching is also enabled.

重要

使用 Microsoft Windows 身份验证下的客户端模拟时, 将在第一个用户访问数据时缓存数据。When you are using client impersonation under Microsoft Windows authentication, the data is cached when the first user accesses the data. 如果其他用户请求相同的数据, 则从缓存中检索数据。If another user requests the same data, the data is retrieved from the cache. 通过对数据库进行另一次调用以验证用户对数据的访问权限, 不会检索数据。The data is not retrieved by making another call to the database to verify the user's access to the data. 如果希望有多个用户访问数据, 并且希望数据库的安全配置验证每个数据的检索, 请不要使用缓存。If you expect more than one user to access the data, and you want each retrieval to the data to be verified by the security configurations for the database, do not use caching.

适用于

另请参阅