Partager via


ControlCachePolicy.Duration Propriété

Définition

Obtient ou définit la durée pendant laquelle les éléments mis en cache doivent rester dans le cache de sortie.

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

Valeur de propriété

Qui TimeSpan représente la durée pendant laquelle un contrôle utilisateur doit rester dans le cache de sortie. La valeur par défaut est Zero.

Exceptions

Le contrôle utilisateur n’est pas associé à un BasePartialCachingControl contrôle et n’est pas mis en cache.

- ou -

La Duration propriété est définie en dehors des étapes d’initialisation et de rendu du contrôle.

Exemples

L’exemple de code suivant montre comment un contrôle utilisateur peut être chargé dynamiquement et manipulé par programmation au moment de l’exécution. L’attribut PartialCachingAttribute est appliqué à un contrôle utilisateur nommé SimpleControl, ce qui signifie qu’il est encapsulé par un PartialCachingControl contrôle au moment de l’exécution. Les SimpleControl paramètres de mise en cache de l’objet peuvent être manipulés par programmation via son objet associé ControlCachePolicy , qui est disponible via une référence au PartialCachingControl contrôle qui l’encapsule. Dans cet exemple, la propriété est examinée pendant l’initialisation Duration de la page et l’expiration du cache est modifiée si certaines conditions sont remplies. Cet exemple fait partie d’un exemple plus large fourni pour la ControlCachePolicy classe.

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

Remarques

Si une stratégie d’expiration absolue est définie à l’aide de la SetSlidingExpiration méthode, la Duration propriété retourne la durée restante jusqu’à l’expiration de l’entrée du cache.

S’applique à

Voir aussi