ControlCachePolicy.Duration 属性

定义

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

public:
 property TimeSpan Duration { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan Duration { get; set; }
member this.Duration : TimeSpan with get, set
Public Property Duration As TimeSpan

属性值

TimeSpan,表示用户控件将在输出缓存中保留的时间。 默认值为 Zero

例外

用户控件与 BasePartialCachingControl 没有关联,因此不可缓存。

- 或 -

Duration 属性是在控件的初始化和呈现阶段之外设置的。

示例

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

<%@ 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>

注解

如果使用 方法设置 SetSlidingExpiration 绝对过期策略,则 Duration 属性将返回缓存条目过期之前的剩余时间量。

适用于

另请参阅