高效网站开发缓存技术应用——网页输出缓存

来源:互联网 发布:视频直播间源码 编辑:程序博客网 时间:2024/06/05 04:26
     网页输出缓存 是 ASP.NET 缓存中的重要组成部分。网页输出缓存又分为:完整页缓存、用户控件缓存 和 缓存后替换。下面我们先来学习一下 ——完整页缓存!

       使用@OutputCache 可以已声明的方式控制ASP.NET页或页中包含的用户控件的输出缓存策略,实现对页面输出缓存的一般性需求。

关键技术:

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@OutputCache Duration="60" Location="Any|Client|Downstream|Server|None|ServerAndClient"   
  2. Shared="True|False" VaryByControl="controlname"   
  3. VaryByCustom="browser|customstring" VaryByHeader="headres"   
  4. VaryByparam="parametername" %>  

@OutputCache 指令中各个属性如下表所示:
   属性名                            说              明Duration缓存过期时间(单位:秒 *注:此属性为必填项)Location指定输出缓存可以使用的场所,默认为 Any(*注:在用户控件中@OutputCache 指令不支持此属性)Shared是否可以由多个页共享,默认值为 FalseVaryByControl一个用分号分割的字符串,用来改变用户控件的部分输出缓存(字符串为用户控件页中的服务器控件ID)VaryByCustom根据自定义文本改变缓存内容VaryByHeader根据HTTP头改变缓存内容VaryByParam一个用分号分割的字符串,用来改变使输出缓存发生变化,默认情况下与GET或POST的参数对应

说明:

@OutputCache 指令必须和 VaryByControl 属性或者 VaryByParam属性之一搭配使用,这是最基本的要求。
例如:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@OutputCache Duration="60" VaryByControl="" %>  
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@OutputCache Duration="60" VaryByParam="" %>  


例子一:根据多个参数进行缓存

               如果要根据多个参数来缓存,需要包含已分号分割的参数名称,例如:VaryByParam="Id,Card".若要根据所有的参数值来缓存,需要将 VaryByParam属性值设为星号,

              例如:VaryByParam = "*"


例子二:实现网页缓存的多个版本方法

               实现网页多个版本缓存有两种方法:一是以@OutputCache 指令属性声明的方式缓存网页输出的多个版本,二是以 HttpCachePolicy 类的属性和方法(Response.Cache)通过编程方式缓存网页输出的多个版本。

例如: 

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Response.Cache.SetExpires(Datetime.Now.AddSecondes(60));  
  2. Response.Cache.SetCacheability(HttpCacheility.Public);  
  3. Response.Cache.SetValidUntilExpires(true);  
  4. Response.Cache.VaryByParam["Card"]=true;  

网页输出缓存 是 ASP.NET 缓存中的重要组成部分。网页输出缓存又分为:完整页缓存、用户控件缓存 和 缓存后替换。接下来下面我们来学习一下 ——用户控件缓存!
       控件缓存的设置实质是对用户控件(“User Control”)的缓存进行缓存配置,注意必须是以用户控件作为缓存目标、对象。
       控件缓存 和 缓存后替换 一起构成了页面部分缓存。两者的缓存模式与结果刚好相反,控件缓存有时也称为分段缓存,只缓存指定区域以此来缓存页输出的部分内容,而缓存后替换不缓存指定区域块,其他为被指定的部分皆被缓存。区别如下图所示:

关键技术:

实现方法一,使用 @OutputCache 指令对用户控件进行缓存,例如:在用户控件.ascx页面头部加入下面的声明语句
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@OutputCache Duration="120" VaryByParam="none"%>  

实现方法二,使用PartialCaching 类,在用户控件的后台代码的头部加入缓存属性说明,主要是用来设置用户控件的缓存时间。例如:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [PartialCaching(10)]  
  2. public partial class WebUserControl:System.Web.UI.UserControl  
  3. {  
  4. ……  
  5. }  

说明:

当Web页面中添加了用户控件,并且都设置了缓存。如果,用户控件的缓存时间大于页面设置的缓存时间,则用户控件则按照自己的缓存时间输出;如果,页面的缓存时间大于用户控件设置的缓存时间,则用户控件、页面都将以页面设置的缓存时间为准,经行缓存输出!

 网页输出缓存 是 ASP.NET 缓存中的重要组成部分。网页输出缓存又分为:完整页缓存、用户控件缓存 和 缓存后替换。接下来下面我们来学习一下 ——缓存后替换!
       缓存后替换(Post-Cache substitution)是Cache2.0新增的缓存功能,它和控件缓存相反,指定的区域没有缓存,而指定的区域之外的部分则被缓存。

关键技术:

       实现缓存后替换的方式有三种:
       A、用Substitution 控件,以声明的方式来实现。例如:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication.Index" %>  
  2. <%@ OutputCache Duration="10" VaryByParam="none" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <meta http-equiv="refresh" content="1;URL=Index.aspx" />  
  7.     <title>使用 Substitution 控件建立缓存后替换</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     我是Substitution控件之外的内容,我缓存了 10秒:  
  12.     <% Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); %><br />  
  13.     <asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime" />  
  14.     </form>  
  15. </body>  
  16. </html>  
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /// <summary>  
  2. /// MethodName 刷新调用方法  
  3. /// </summary>  
  4. /// <param name="context"></param>  
  5. /// <returns></returns>  
  6. public static String GetTime(HttpContext context)  
  7. {  
  8.     return "我是Substitution控件的内容,我没缓存 更新频率1秒:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  
  9. }  
     B、用Substitution 控件,以程序化API方式来实现。例如:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs"   
  2.     Inherits="WebApplication.Index" %>  
  3. <%@ OutputCache Duration="10" VaryByParam="none" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <meta http-equiv="refresh" content="1;URL=Index.aspx" />  
  8.     <title>使用 Substitution 控件建立缓存后替换</title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.     我是Substitution控件之外的内容,我缓存了 10秒:  
  13.     <% Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); %><br />  
  14.     我需要适时更新,不能缓存:<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
  15.     </form>  
  16. </body>  
  17. </html>  
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. protected void Page_Load(object sender, EventArgs e)  
  2.  {  
  3.      Substitution sub = new Substitution();  
  4.      sub.MethodName = "GetTime";  
  5.      this.PlaceHolder1.Controls.Add(sub);  
  6.  }  
  7.   
  8.  /// <summary>  
  9.  /// MethodName 刷新调用方法  
  10.  /// </summary>  
  11.  /// <param name="context"></param>  
  12.  /// <returns></returns>  
  13.  public static String GetTime(HttpContext context)  
  14.  {  
  15.      return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  
  16.  }  
   C、用AdRotator控件,以AdRotator控件隐式实现。使用AdRotator控件实现缓存后替换很方便,主要原因是该控件本身具支持缓存后替换。将其 AdvertisementFile 属性指定          对应的广告 轮显 XML文件。例如:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <%@ OutputCache Duration="10" VaryByParam="None" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>使用AdRotator控件实现缓存后替换</title>  
  7.     <meta http-equiv="refresh" content="1;URL=Default.aspx" />  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     我是AdRotator控件之外的内容,我缓存了 10秒:  
  12.     <% Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); %><br />  
  13.     <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/App_Data/Ad.xml" />  
  14.     </form>  
  15. </body>  
  16. </html>  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File">  
  3.   <Ad>  
  4.     <ImageUrl>~/images/ASP.NET编程词典.bmp</ImageUrl>  
  5.     <NavigateUrl>http://www.mrbccd.com</NavigateUrl>  
  6.     <AlternateText>编程词典网</AlternateText>  
  7.     <Impressions>100</Impressions>  
  8.   </Ad>  
  9.   <Ad>  
  10.     <ImageUrl>~/images/视频学.bmp</ImageUrl>  
  11.     <NavigateUrl>http://www.mingribook.com</NavigateUrl>  
  12.     <AlternateText>明日图书网</AlternateText>  
  13.     <Impressions>50</Impressions>  
  14.   </Ad>  
  15.   <Ad>  
  16.     <ImageUrl>~/images/典型模块.bmp</ImageUrl>  
  17.     <NavigateUrl>http://www.mingribook.com</NavigateUrl>  
  18.     <AlternateText>明日图书网</AlternateText>  
  19.     <Impressions>50</Impressions>  
  20.   </Ad>  
  21. </Advertisements>  






1 0