语言

ControlCachePolicy 类

定义

提供对 ASP.NET 用户控件的输出缓存设置的编程访问。

public ref class ControlCachePolicy sealed
public sealed class ControlCachePolicy
type ControlCachePolicy = class
Public NotInheritable Class ControlCachePolicy
继承
ControlCachePolicy

示例

下面的代码示例演示了如何在运行时以编程方式动态加载用户控件和以编程方式操作。 该 PartialCachingAttribute 属性应用于名为 SimpleControl的用户控件,这意味着用户控件在运行时由 PartialCachingControl 控件包装。 SimpleControl可以通过对象的关联ControlCachePolicy对象以编程方式操作对象的缓存设置,该对象可通过对包装它的控件的引用PartialCachingControl获得。 在此示例中, Duration 在页面初始化期间检查属性,并在满足某些条件时使用 SetSlidingExpiration 和 SetExpires 方法进行更改。

<%@ Page Language="C#" %>
<%@ Reference Control="SimpleControl.ascx" %>
<script language="C#" runat="server">

// The following example demonstrates how to load a user control dynamically at run time, and
// work with the ControlCachePolicy object associated with it.

// Loads and displays a UserControl defined in a seperate Logonform.ascx file.
// You need to have "SimpleControl.ascx" file in 
// the same directory as the aspx file. 

void Page_Init(object sender, System.EventArgs e) {
    
    // Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
    PartialCachingControl pcc = LoadControl("SimpleControl.ascx") as PartialCachingControl;        
    
    // If the control is slated to expire in greater than 60 Seconds
    if (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60) ) 
    {        
        // Make it expire faster. Set a new expiration time to 30 seconds, and make it
        // an absolute expiration if it isnt already.        
        pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)));
        pcc.CachePolicy.SetSlidingExpiration(false);
    }                    
    Controls.Add(pcc);
}
</script>
<%@ Page Language="VB" %>
<%@ Reference Control="SimpleControl.ascx" %>
<script language="VB" runat="server">

    ' The following example demonstrates how to load a user control dynamically at run time, and
    ' work with the ControlCachePolicy object associated with it.

    ' Loads and displays a UserControl defined in a seperate Logonform.ascx file.
    ' You need to have "SimpleControl.ascx" file in 
    ' the same directory as the aspx file. 

    Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    
        ' Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
        Dim pcc As PartialCachingControl
        pcc = LoadControl("SimpleControl.ascx")
    
        ' If the control is slated to expire in greater than 60 Seconds
        If (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60)) Then
            ' Make it expire faster. Set a new expiration time to 30 seconds, and make it
            ' an absolute expiration if it isnt already.        
            pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)))
            pcc.CachePolicy.SetSlidingExpiration(False)
        End If
        Controls.Add(pcc)
    End Sub
</script>

下面的代码示例演示如何使用 SimpleControl Web 窗体页中的用户控件。 若要成功运行此示例,请确保用户控件文件(.ascx)、其代码隐藏文件(.cs或.vb),以及承载用户控件(.aspx)的 Web 窗体页面位于同一目录中。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

[PartialCaching(100)]
public partial class SimpleControl : System.Web.UI.UserControl
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        ItemsRemaining.Text = GetAvailableItems().ToString();
        CacheTime.Text = DateTime.Now.ToLongTimeString();
    }

    private int GetAvailableItems()
    {
        SqlConnection sqlConnection = new SqlConnection
            ("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
        SqlCommand sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.CommandText = "GetRemainingItems";
        sqlConnection.Open();
        int items = (int)sqlCommand.ExecuteScalar();
        sqlConnection.Close();
        return items;
    }
}
Imports System.Data.SqlClient

Partial Class SimpleControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ItemsRemaining.Text = GetAvailableItems().ToString()
        CacheTime.Text = DateTime.Now.ToLongTimeString()
    End Sub

    Private Function GetAvailableItems() As Integer
        Dim sqlConnection As SqlConnection
        Dim sqlCommand As SqlCommand
        Dim items As Integer

        sqlConnection = New SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;")
        sqlCommand = sqlConnection.CreateCommand()
        sqlCommand.CommandType = Data.CommandType.StoredProcedure
        sqlCommand.CommandText = "GetRemainingItems"
        sqlConnection.Open()
        items = CInt(sqlCommand.ExecuteScalar())
        sqlConnection.Close()
        Return items
    End Function
End Class
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleControl.ascx.cs" Inherits="SimpleControl" %>
<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
    <asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
    <asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Control Language="VB" AutoEventWireup="true" CodeFile="SimpleControl.ascx.vb" Inherits="SimpleControl" %>
<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
    <asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
    <asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>

注解

开发人员 ControlCachePolicy 在编程用户控件方案中使用该类来指定用户控件(.ascx 文件)的输出缓存设置。 ASP.NET 在实例中 BasePartialCachingControl 嵌入用户控件。 该 BasePartialCachingControl 类表示启用了输出缓存的用户控件。 访问 BasePartialCachingControl.CachePolicy 控件的属性 PartialCachingControl 时,始终会收到一个有效的 ControlCachePolicy 对象。 但是,如果访问UserControl.CachePolicy控件的属性UserControl,则仅当用户控件已由ControlCachePolicy控件包装时,才会收到有效的BasePartialCachingControl对象。 如果未包装, ControlCachePolicy 属性返回的对象将在尝试操作它时引发异常,因为它没有关联的 BasePartialCachingControl对象。 若要确定实例是否 UserControl 支持缓存而不生成异常,请检查该 SupportsCaching 属性。

使用 ControlCachePolicy 此类是启用输出缓存的几种方法之一。 以下列表描述了可用于启用输出缓存的方法:

  • 使用 @ OutputCache 指令在声明性方案中启用输出缓存。

  • 使用 PartialCachingAttribute 特性为代码隐藏文件中的用户控件启用缓存。

  • 使用 ControlCachePolicy 类在编程方案中指定缓存设置,在该方案中,你正在使用 BasePartialCachingControl 以前方法之一启用缓存的实例,并使用该方法动态加载 TemplateControl.LoadControl 这些实例。 ControlCachePolicy只能在控制生命周期的阶段和Init阶段之间PreRender成功操作实例。 如果在阶段后ControlCachePolicy修改对象PreRender,ASP.NET 将引发异常,因为呈现控件后所做的任何更改实际上都不会影响缓存设置(在Render阶段中缓存控件)。 最后,用户控件实例(因此其 ControlCachePolicy 对象)仅在实际呈现时可用于编程操作。

属性

名称 说明
Cached

获取或设置一个值,该值指示是否为用户控件启用片段缓存。

Dependency

获取或设置与缓存的用户控件输出关联的类的 CacheDependency 实例。

Duration

获取或设置缓存项保留在输出缓存中的时间量。

ProviderName

获取或设置与控件实例关联的输出缓存提供程序的名称。

SupportsCaching

获取一个值,该值指示用户控件是否支持缓存。

VaryByControl

获取或设置控件标识符列表,以改变缓存的输出。

VaryByParams

获取或设置一个列表 GET 或 POST 参数名称,以改变缓存的输出。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type。

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
SetExpires(DateTime)

BasePartialCachingControl指示包装用户控件的控件在指定的日期和时间使缓存项过期。

SetSlidingExpiration(Boolean)

BasePartialCachingControl指示包装用户控件的控件将用户控件的缓存项设置为使用滑动或绝对过期。

SetVaryByCustom(String)

设置输出缓存将用于改变用户控件的自定义字符串的列表。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅