Object Caching Item Dependency : Item Dependency « Cache « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » Cache » Item Dependency 
13. 8. 2. 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>
13. 8. Item Dependency
13. 8. 1. cached with a dependency on an item in the cache named Products.
13. 8. 2. Object Caching Item Dependency
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.