ControlCachePolicy.Duration Propiedad
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene o establece la cantidad de tiempo que los elementos almacenados en caché deben permanecer en la memoria caché de salida.
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
Valor de propiedad
que TimeSpan representa la cantidad de tiempo que un control de usuario debe permanecer en la memoria caché de salida. El valor predeterminado es Zero.
Excepciones
El control de usuario no está asociado a y BasePartialCachingControl no se puede almacenar en caché.
O bien
La Duration propiedad se establece fuera de las fases de inicialización y representación del control.
Ejemplos
En el ejemplo de código siguiente se muestra cómo se puede cargar un control de usuario de forma dinámica y manipulada mediante programación en tiempo de ejecución. El PartialCachingAttribute atributo se aplica a un control de usuario denominado SimpleControl, lo que significa que se ajusta mediante un PartialCachingControl control en tiempo de ejecución. La SimpleControl configuración de almacenamiento en caché del objeto se puede manipular mediante programación a través de su objeto asociado ControlCachePolicy , que está disponible a través de una referencia al PartialCachingControl control que lo encapsula. En este ejemplo, la propiedad se examina durante la Duration inicialización de páginas y se cambia la expiración de la memoria caché si se cumplen algunas condiciones. Este ejemplo forma parte de un ejemplo más grande proporcionado para la ControlCachePolicy clase .
<%@ 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>
Comentarios
Si se establece una directiva de expiración absoluta mediante el SetSlidingExpiration método , la Duration propiedad devuelve la cantidad de tiempo restante hasta la expiración de la entrada de caché.