File Dependent Data Caching

This demo highlights file dependent data caching. The content in the DataList below is populated from an XML file. The contents of the XML file, however, are cached with a file dependency on the actual on-disk XML file, meaning that the XML cache will be invalidated when the XML file on-disk is edited.


Links
Bazquirk
Blah
Foobar
Ipsum
Dolor


Source Code
<%@Import Namespace="System.Data"%>
<script runat="server" language="C#">
void Page_Load(Object sender, EventArgs e)
{
  BindDestinations();
}

private void BindDestinations()
    {    
      DataView dv = (DataView) Cache["Destinations"];

      if(dv == null)
      {
        DataSet ds = new DataSet();
        string path = Request.MapPath (Request.ApplicationPath); 
        ds.ReadXml (path + "\\demos\\Destinations.xml",XmlReadMode.InferSchema);
        dv = ds.Tables[0].DefaultView;    
        Cache.Insert("Destinations", dv, new CacheDependency (path + "\\demos\\Destinations.xml"));
        lblTime.Text = "Data was loaded from the cache at " + DateTime.Now.ToString() + ".";
      }

      DataList1.DataSource = dv;
      DataBind();
    }
</script>

<asp:DataList DataKeyField="LinkText" id="DataList1" runat="server" Width="100px">
        <HeaderStyle BackColor="#aaaadd" Font-Bold="True" Font-Name="Arial" Font-Size="12"></HeaderStyle>
        <AlternatingItemStyle BackColor="#FFFFCC" Font-Name="Arial" Font-Size="8"></AlternatingItemStyle>
        <ItemStyle BackColor="#FFFFFF" Font-Name="Arial" Font-Size="8"></ItemStyle>
        <HeaderTemplate>
          Links
        </HeaderTemplate>
        <ItemTemplate>
          <asp:HyperLink id=HyperLink1 Target="_top" Text='<%# DataBinder.Eval(Container.DataItem, "LinkText") %>' NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "NavigateURL") %>' runat="server" >
          </asp:HyperLink>
        </ItemTemplate>
        <SelectedItemTemplate>
          <asp:HyperLink id=HyperLink2 Text='<%# DataBinder.Eval(Container.DataItem, "LinkText") %>' NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "NavigateURL") %>' runat="server" >
          </asp:HyperLink>
        </SelectedItemTemplate>
      </asp:DataList>
      <br>
      <asp:Label id="lblTime" runat="server" Width="450px" Font-Bold="True" Font-Names="Tahoma"></asp:Label>


Return to the article