ControlCachePolicy.SetSlidingExpiration(Boolean) メソッド

定義

ユーザー コントロールをラップする BasePartialCachingControl コントロールに対して、ユーザー コントロールのキャッシュ エントリで変化する有効期限または絶対有効期限が使用されるように設定するよう指示します。

public:
 void SetSlidingExpiration(bool useSlidingExpiration);
public void SetSlidingExpiration (bool useSlidingExpiration);
member this.SetSlidingExpiration : bool -> unit
Public Sub SetSlidingExpiration (useSlidingExpiration As Boolean)

パラメーター

useSlidingExpiration
Boolean

絶対有効期限ではなく変化する有効期限を使用する場合は true。それ以外の場合は false

例外

ユーザー コントロールは、BasePartialCachingControl に関連付けられておらず、キャッシュに保存することはできません。

次のコード例では、実行時にユーザー コントロールを動的に読み込み、プログラムで操作する方法を示します。 という名前 SimpleControl のユーザー コントロールは 属性で PartialCachingAttribute 装飾されます。つまり、実行時にコントロールによって PartialCachingControl ラップされます。 オブジェクトのキャッシュ設定は SimpleControl 、関連付けられている ControlCachePolicy オブジェクトを通じてプログラムで操作できます。これは、ラップするコントロールへの PartialCachingControl 参照を通じて使用できます。 この例では、ページの初期化中にキャッシュ設定が調べられ、一部の条件が満たされた場合に変更されます。 この例は、 クラスに対して提供される大きな例の 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>

注釈

SetExpiresメソッドと SetSlidingExpiration メソッド (渡しtrue) を使用して、絶対有効期限ポリシーではなくスライディング有効期限キャッシュ ポリシーを使用するようにユーザー コントロールをラップするコントロールに指示BasePartialCachingControlします。 SetExpiresメソッドと メソッド (渡しfalse) をSetSlidingExpiration使用して、絶対有効期限ポリシーを指定します。

適用対象

こちらもご覧ください