ASP.NET Tutorial/Cache/Introduction

Материал из .Net Framework эксперт
Версия от 14:57, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Basic operations executed on the ASP.NET Cache object

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

   Inherits="Default" %>

<!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>Testing the Cache</title>

</head> <body>

       <form id="form1" runat="server">
           <asp:Button ID="Button1" runat="server" Text="Add a new item..." OnClick="Add_Click" />
           <asp:Button ID="Button2" runat="server" Text="Read the item..." OnClick="Read_Click" />
           <asp:Button ID="Button3" runat="server" Text="Remove the item..." OnClick="Remove_Click" />

           <asp:Label runat="server" ID="Label1" />
       </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page {

   protected void Read_Click(object sender, EventArgs e)
   {
       string msg = String.Format("{0} ({1})", 
           Cache.Get("MyData"), 
           Cache.Get("MyData").GetType().ToString());
       Label1.Text = msg;
   }
   protected void Add_Click(object sender, EventArgs e)
   {
       Cache["MyData"] = DateTime.Now;
   }
   protected void Remove_Click(object sender, EventArgs e)
   {
       object obj = Cache.Remove("MyData");
       Label1.Text = obj.GetType().ToString();
   }

}</source>


Cache Object DataSource

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CacheObjectDataSource" %> <!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>Cached object data source</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:GridView ID="_peopleGridView" runat="server" 
           DataSourceID="_personObjectDataSource"
           EnableViewState="False" />
       <asp:ObjectDataSource ID="_personObjectDataSource" runat="server" 
           CacheDuration="120"
           EnableCaching="True" SelectMethod="GetPeople" DataObjectTypeName="Person"
           TypeName="SampleData" />    
   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; 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; public partial class CacheObjectDataSource : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
       Response.Output.Write("objds.GetHashCode() = {0}
", _personObjectDataSource.GetType().GetHashCode()); Response.Output.Write("objds.CacheDuration = {0}
", _personObjectDataSource.CacheDuration); Response.Output.Write("objds.CacheExpirationPolicy = {0}
", _personObjectDataSource.CacheExpirationPolicy); Response.Output.Write("objds.SqlCacheDependency = {0}
", _personObjectDataSource.SqlCacheDependency); Response.Output.Write("objds.Typename = {0}
", _personObjectDataSource.TypeName); Response.Output.Write("objds.SelctMethod = {0}
", _personObjectDataSource.SelectMethod); }

} File: Person.cs using System; using System.Data; using System.Collections.Generic; using System.Collections; 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; public class Person {

   private int _age;
   private string _name;
   private bool _isMarried;
   private DateTime _birthDay;
   public int Age
   {
       get { return _age; }
       set { _age = value; }
   }
   public string Name
   {
       get { return _name; }
       set { _name = value; }
   }
   public bool IsMarried
   {
       get { return _isMarried; }
       set { _isMarried = value; }
   }
   public DateTime BirthDay
   {
       get { return _birthDay; }
       set { _birthDay = value; }
   }
   public Person() { }
   public Person(int age, string name, bool isMarried, DateTime birthDay)
   {
       _age = age;
       _name = name;
       _isMarried = isMarried;
       _birthDay = birthDay;
   }

} public static class SampleData {

   public static ICollection<Person> GetPeople()
   {
       List<Person> ret = new List<Person>();
       for (int i = 0; i < 10; i++)
           ret.Add(new Person(i + 20, "Person " + i.ToString(), (i % 2) == 0, 
                              DateTime.Now.AddYears(i-40)));
       
       return ret;
   }

}</source>


Convert data in Cache to integer

   <source lang="csharp">

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!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>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       
<asp:Label ID="Label1" runat="server" Width="147px"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" Width="98px" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Table ID="Table1" runat="server" Height="88px" Width="166px"> </asp:Table>
<asp:Button ID="Button2" runat="server" Text="Button" Width="93px" />
   </form>

</body> </html> File: Default.aspx.vb

Partial Class _Default

   Inherits System.Web.UI.Page
   
   Private Property myCounter() As Integer
       Get
           Return CInt(Cache("myCounter"))
       End Get
       Set(ByVal Value As Integer)
           Cache("myCounter") = Value
       End Set
   End Property
   Private ReadOnly Property RowTexts() As ArrayList
       Get
           Dim al As ArrayList
           al = CType(Cache("rowTexts"), ArrayList)
           If IsNothing(al) Then
               al = New ArrayList()
               Cache("rowTexts") = al
           End If
           Return al
       End Get
   End Property
   Private Sub AddARow(ByVal s As String)
       Dim cell As New TableCell()
       Dim row As New TableRow()
       cell.Text = s
       row.Cells.Add(cell)
       Table1.Rows.Add(row)
   End Sub
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       myCounter += 1
       Label1.Text = myCounter.ToString()
   End Sub
   Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
       RowTexts.Add(TextBox1.Text)
       AddARow(TextBox1.Text)
   End Sub
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim s As String
       For Each s In RowTexts
           AddARow(s)
       Next
   End Sub

End Class</source>


Overview of Caching

   <source lang="csharp">

The ASP.NET 3.5 Framework supports the following types of caching: Page Output Caching caches an entire page. Partial Page Caching caches only particular regions of a page. DataSource Caching caches different ASP.NET DataSource controls such as the SqlDataSource and ObjectDataSource controls. Data Caching caches arbitrary objects in memory.</source>


Programmatic Fragment Caching

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ProgrammaticFragmentCaching" %> <!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>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Label runat="server" ID="_timestampLabel" EnableViewState="false" />

   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; 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; public partial class ProgrammaticFragmentCaching : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
       _timestampLabel.Text = "This page generated at " + DateTime.Now.ToLongTimeString();
       Response.Cache.SetExpires(DateTime.Now + new TimeSpan(0, 1, 0));
   }

}</source>