ASP.NET Tutorial/Cache/Item Dependency

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

cached with a dependency on an item in the cache named Products.

File: Default.aspx
<%@ Page Language="C#" %>
<%@ OutputCache Duration="3600" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Cache.Insert("Products", DateTime.Now);
        Response.AddCacheItemDependency("Products");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Product List Key Dependency</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%= DateTime.Now.ToString("T") %>
    <hr />
    <asp:GridView
        id="grdProducts"
        DataSourceID="srcProducts"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Title, Director FROM Products"
        Runat="server" />
   <br /><br />
   <a href="NextPage.aspx">Add Product</a>
    </div>
    </form>
</body>
</html>
File: NextPage.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void dtlProduct_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        Cache.Remove("Products");
        Response.Redirect("~/Default.aspx");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Add Product Key Dependency</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Add Product</h1>
    <asp:DetailsView
        id="dtlProduct"
        DefaultMode="Insert"
        DataSourceID="srcProducts"
        AutoGenerateRows="false"
        AutoGenerateInsertButton="true"
        Runat="server" OnItemInserted="dtlProduct_ItemInserted">
        <Fields>
        <asp:BoundField
            DataField="Title"
            HeaderText="Title:" />
        <asp:BoundField
            DataField="Director"
            HeaderText="Director:" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        InsertCommand="INSERT Products (Title, Director)
            VALUES (@Title, @Director)"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Object Caching Item Dependency

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Object Caching</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Object Caching</h1>
    <h2>Item Dependency</h2>
     <asp:Label ID="lblMessage" runat="server" />
     <br />
     <br />
     <asp:GridView ID="gv" runat="server" />
     <br />
     <asp:Button ID="btnClear" runat="server" Text="Clear Cache" OnClick="btnClear_Click" />
     <asp:Button ID="btnPost" runat="server" Text="Post" />
     <br />
     <br />
     <asp:Button ID="btnInit" runat="server" Text="Initialize Keys" OnClick="btnInit_Click" />
     <asp:Button ID="btnKey0" runat="server" Text="Change Key 0" OnClick="btnKey0_Click" />
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;      //  necessary for CacheDependency
using System.Xml;            //  necessary for Xml stuff
public partial class Default_aspx : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
     CreateGridView();
   }
   private void CreateGridView()
   {
     DataSet dsGrid;
     dsGrid = (DataSet)Cache["GridViewDataSet"];
     if (dsGrid == null)
     {
       dsGrid = GetDataSet();
       string[] fileDependsArray = {Server.MapPath("Data.xml")};
       string[] cacheDependsArray = {"Depend0", "Depend1", "Depend2"};
       CacheDependency cacheDepends = new CacheDependency
                      (fileDependsArray, cacheDependsArray);
       Cache.Insert("GridViewDataSet", dsGrid, cacheDepends);
       lblMessage.Text = "Data from XML file.";
     }
     else
     {
       lblMessage.Text = "Data from cache.";
     }
     gv.DataSource = dsGrid.Tables[0];
     gv.DataBind();
   }
   private DataSet GetDataSet()
   {
     DataSet dsData = new DataSet();
     XmlDataDocument doc = new XmlDataDocument();
     doc.DataSet.ReadXml(Server.MapPath("Data.xml"));
     dsData = doc.DataSet;
     return dsData;
   }
  protected void btnClear_Click(object sender, EventArgs e)
   {
      Cache.Remove("GridViewDataSet");
    CreateGridView();
   }
  protected void btnInit_Click(object sender, EventArgs e)
  {
    Cache["Depend0"] = "This is the first dependency.";
    Cache["Depend1"] = "This is the 2nd dependency.";
    Cache["Depend2"] = "This is the 3rd dependency.";
  }
  protected void btnKey0_Click(object sender, EventArgs e)
  {
    Cache["Depend0"] = "This is a changed first dependency.";
  }
}
 
File: NorthWind.xml
<?xml version="1.0" encoding="UTF-16" ?>
<ROOT>
  <Customers CustomerID="A" CompanyName="A" ContactName="Dan" ContactTitle="Sales" Address="Str. 57" City="Berlin" PostalCode="11111" Country="Germany" Phone="030-0074321" Fax="030-0076545" /> 
  <Customers CustomerID="B" CompanyName="B" ContactName="Ana" ContactTitle="Owner" Address="Road 22" City="New York" PostalCode="00000" Country="Mexico" Phone="(555) 555-4729" Fax="(5) 555-3745" /> 
</ROOT>