当前位置:中国站长下载文章中心网页编程.NET编程 → ASP.NET 2.0 中改进的缓存功能

ASP.NET 2.0 中改进的缓存功能

减小字体 增大字体 作者:不详  来源:不详  发布时间:2006-8-14 9:55:54
个 SqlCacheDependency 对象实例。使用 Insert 方法在 Cache 中插入新对象时,可以使用 SqlCacheDependency 对象。
  
  例如,列表 6 中的页面显示了 Titles 数据库表中的记录数。计数是基于对基础数据库表的依赖关系进行缓存的。
  
  列表 6:DisplayTitleCount.aspx (C#)
  
  <%@ Page Language="c#" %>
  <%@ Import Namespace="System.Data.SqlClient" %>
  <script runat="server">
  
  void Page_Load()
   {
  int count = 0;
  
  if (Cache["TitleCount"] != null)
   {
  count = (int)Cache["TitleCount"];
   }
  else
   {
  string connectionString =
  ConfigurationSettings.ConnectionStrings[
  "mySqlServer"].ConnectionString;
  SqlConnection con = new SqlConnection(connectionString);
  SqlCommand cmd = new
  SqlCommand("SELECT Count(*) FROM Titles", con);
  con.Open();
  count = (int)cmd.ExecuteScalar();
  con.Close();
  Cache.Insert("TitleCount", count,
  new SqlCacheDependency("Pubs", "Titles"));
   }
  lblTitleCount.Text = count.ToString();
   }
  
  </script>
  
  <html>
  <head runat="server">
  <title>Display Title Count</title>
  </head>
  <body>
  <form id="form1" runat="server">
  
  <asp:Label ID="lblTitleCount" Runat="Server" />
  
  </form>
  </body>
  </html>
  
  列表 6:DisplayTitleCount.aspx (Visual Basic .NET)
  
  <%@ Page Language="vb" %>
  <%@ Import Namespace="System.Data.SqlClient" %>
  <script runat="server">
  
  Sub Page_Load()
  
  Dim count As Integer = 0
  
  If Not Cache("TitleCount") Is Nothing Then
  count = Convert.ToInt32(Cache("TitleCount"))
  Else
  Dim connectionString As String = _
  ConfigurationSettings.ConnectionStrings( _
  "mySqlServer").ConnectionString
  Dim con As New SqlConnection(connectionString)
  Dim cmd As New _
  SqlCommand("SELECT Count(*) FROM Titles", con)
  con.Open()
  count = Convert.ToInt32(cmd.ExecuteScalar())
  con.Close()
  Cache.Insert("TitleCount", count, _
  new SqlCacheDependency("Pubs", "Titles"))
  End If
  
  lblTitleCount.Text = count.ToString()
  End Sub
  
  </script>
  
  <html>
  <head id="Head1" runat="server">
  <title>Display Titles Count</title>
  </head>
  <body>
  <form id="form1" runat="server">
  
  <asp:Label ID="lblTitleCount" Runat="Server" />
  
  </form>
  </body>
  </html>
  
  返回页首
  使用 Post-Cache Substitution
  在许多情况下,您需要缓存页面的一部分,而不是整个页面。例如,在您的 Web 站点主页上,您可能希望同时显示随机的标题广告和数据库表中的记录。如果缓存整个页面,每个用户都将在每次请求的页面上看到同一个标题广告。
  
  要处理这种同时混有动态内容和缓存内容的问题,一种方法是使用 Web 用户控件。因为可以为 Web 用户控件添加 OutputCache 指令,所以即使不缓存包含页面的内容,也可以缓存 Web 用户控件的内容。
  
  但有时候可能会事与愿违。虽然您可以使用 Web 用户控件在动态页面上添加缓存的内容,但很多情况下,您实际上是想在缓存的页面中添加动态内容。例如,假设您要缓存整个页面的内容,只留一小块区域用于显示当前用户的用户名。这种情况下最好使用 Post-Cache Substitution。
  
  ASP.NET 2.0 Framework 引入了一种新控件,称为 Substitution 控件。您可以使用 Substitution 控件在缓存的页面中插入动态内容。列表 7 中的页面使用 Substitution 控件将用户名插入到缓存的内容中(参见图 3)。
  
  图 3:使用 Substitution 控件显示用户名
  
  
  列表 7:PostCacheSubstitution.aspx (C#)
  
  <%@ Page Language="C#" %>
  <%@ OutputCache Duration="6000" VaryByParam="none" %>
  
  <script runat="server">
  
  static string DisplayUsername(HttpContext context)
   {
  if (!context.Request.IsAuthenticated)
  return "Anonymous";
  else
  return context.User.Identity.Name;
   }
  
  </script>
  
  <html>
  <head runat="server">
  <title>Post Cache Substitution</title>
  </head>
  <body>
  <form id="form1" runat="server">
  
  Welcome <asp:Substitution
  MethodName="DisplayUsername" Runat="Server" />!
  
  <p>
  此页已缓存, 因为时间
  <%= DateTime.Now.ToString("t") %> 并无改变。
  </p>
  
  </form>
  </body>
  </html>
  
  列表 7:PostCacheSubstitution.aspx (Visual Basic .NET)
  
  <%@ Page Language="vb" %>
  <%@ OutputCache Duration="6000" VaryByParam="none" %>
  
  <script runat="server">
  
  Shared Function DisplayUsername(ByVal context As HttpContext) _
  As String
  If Not context.Request.IsAuthenticated Then
  Return "Anonymous"
  Else
  Return context.User.Identity.Name
  End If
  End Function
  
  </script>
  
  <html>
  <head id="Head1" runat="server">
  <title>Post Cache Substitution</title>
  </head>
  <body>
  <form id="form1" runat="server">
  
  Welcome <asp:Substitution
  ID="Substitution1"
  MethodName="DisplayUsername"
  Runat="Server" />!
  
  <p>
  此页已缓存, 因为时间
  <%= DateTime.Now.ToString("t") %> 并无改变。
  </p>
  
  </form>
  </body>
  </html>
  
  Substitution 控件有一个非常重要的属性:即 MethodName 属性。MethodName 属性用于表示为返回动态内容而调用的方法。由 Substitution 控件调用的方法必须是静态方法(在 Visual Basic .NET 中共享的方法)。此外,该方法还必须具有一个表示当前 HttpContext 的参数。
  
  在 ASP.NET 2.0 Framework 中,已对 AdRotator 控件进行了修改以支持 Post-Cache Substitution。如果在使用 OutputCache 指令的页面中添加 AdRotator 控件,AdRotator 控件将自动从其包含页面的缓存策略中排除出去。
  
  返回页首
  结论
  缓存对由数据库驱动的 Web 应用程序的性能具有非常大的影响。幸运的是,ASP.NET 2.0 Framework 提供了许多新的重要增强功能,使您可以在应用程序中更轻松地使用缓存功能。
  
  新增的 DataSource 控件中包含的属性使得在内存中缓存数据库数据变得非常容易。通过使用 DataSource 控件,您可以检索并缓存数据库数据,而无需编写任何代码。
  
  新增的 SQL Cache Invalidation 支持使您可以在基础数据库中的数据发生更改时自动在缓存中重新加载数据库数据。此功能为您提供了缓存的所有性能优势,而不用担心数据过期的问题。
  
  最后,使用新增的 Substitution 控件,您可以更轻松地在缓存的页面中混

上一页  [1] [2] [3] [4] [5]  下一页