XmlDataSource.EnableCaching Propriedade

Definição

Recebe ou define um valor que indica se o controlo XmlDataSource tem a cache de dados ativada.

public:
 virtual property bool EnableCaching { bool get(); void set(bool value); };
public virtual bool EnableCaching { get; set; }
member this.EnableCaching : bool with get, set
Public Overridable Property EnableCaching As Boolean

Valor de Propriedade

true se a cache de dados estiver ativada para o controlo de fonte de dados; caso contrário, false. O valor predefinido é true.

Exemplos

O exemplo de código seguinte demonstra como ativar a cache ao usar o XmlDataSource controlo para exibir dados contidos num ficheiro XML. A cache é ativada quando a EnableCaching propriedade é definida como true e o CacheDuration é definido para o número de segundos em que os dados são armazenados em cache pelo controlo de fonte de dados.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml"
        enablecaching="True"
        cacheduration="60"
        cacheexpirationpolicy="Sliding" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml"
        enablecaching="True"
        cacheduration="60"
        cacheexpirationPolicy="Sliding" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>

O ficheiro XML no exemplo de código tem os seguintes dados:

<books>
   <computerbooks>
     <book title="Secrets of Silicon Valley" author="Sheryl Hunter"/>
     <book title="Straight Talk About Computers" author="Dean Straight"/>
     <book title="You Can Combat Computer Stress!" author="Marjorie Green"/>
   </computerbooks>
   <cookbooks>
     <book title="Silicon Valley Gastronomic Treats" author="Innes del Castill"/>
   </cookbooks>
</books>

Observações

O XmlDataSource controlo armazena automaticamente em cache os dados quando a EnableCaching propriedade está definida para true. Por defeito, a CacheDuration propriedade está definida para 0, o que indica uma cache indefinida, e a fonte de dados irá armazenar dados em cache até que o ficheiro XML de que depende seja alterado. Se definir a CacheDuration propriedade para um valor superior a 0, a cache armazena dados durante esse número de segundos antes de recuperar um novo conjunto.

Se a EnableCaching propriedade for definida como true e a TransformArgumentList propriedade estiver definida, as entradas da cache não são automaticamente invalidadas quando os parâmetros mudam na lista de argumentos transforms. Nesse caso, tens de escrever código para invalidar a cache definindo a CacheKeyDependency propriedade.

Aplica-se a

Ver também