ControlCachePolicy.Duration Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o tempo que os itens em cache devem permanecer na cache de saída.
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 Propriedade
A que representa o tempo que TimeSpan um utilizador controla para permanecer na cache de saída. A predefinição é Zero.
Exceções
O controlo do utilizador não está associado a um BasePartialCachingControl e não é cacheável.
-ou-
A Duration propriedade é definida fora das fases de inicialização e renderização do controlo.
Exemplos
O exemplo de código seguinte demonstra como um controlo do utilizador pode ser carregado dinamicamente e manipulado programaticamente em tempo de execução. O PartialCachingAttribute atributo é aplicado a um controlo de utilizador chamado SimpleControl, o que significa que é envolto por um PartialCachingControl controlo em tempo de execução. As SimpleControl definições de cache do objeto podem ser manipuladas programaticamente através do objeto associado ControlCachePolicy , que está disponível através de uma referência ao PartialCachingControl controlo que o envolve. Neste exemplo, a Duration propriedade é examinada durante a inicialização da página e a expiração do cache é alterada se algumas condições forem cumpridas. Este exemplo faz parte de um exemplo mais amplo fornecido à ControlCachePolicy turma.
<%@ 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>
Observações
Se uma política de expiração absoluta for definida usando o SetSlidingExpiration método, a Duration propriedade devolve o tempo restante até à expiração da entrada do cache.