Share via


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 プロパティはキャッシュ エントリの有効期限が切れるまで残りの時間を返します。

適用対象

こちらもご覧ください