高效网站开发缓存技术应用——网页输出缓存(Ⅲ)
网页输出缓存 是 ASP.NET 缓存中的重要组成部分。网页输出缓存又分为:完整页缓存、用户控件缓存 和 缓存后替换。接下来下面我们来学习一下 ——缓存后替换!
缓存后替换(Post-Cache substitution)是Cache2.0新增的缓存功能,它和控件缓存相反,指定的区域没有缓存,而指定的区域之外的部分则被缓存。
关键技术:
实现缓存后替换的方式有三种:
A、用Substitution 控件,以声明的方式来实现。例如:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication.Index" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>
<!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">
<meta http-equiv="refresh" content="1;URL=Index.aspx" />
<title>使用 Substitution 控件建立缓存后替换</title>
</head>
<body>
<form id="form1" runat="server">
我是Substitution控件之外的内容,我缓存了 10秒:
<% Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); %><br />
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime" />
</form>
</body>
</html>
/// <summary>
/// MethodName 刷新调用方法
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static String GetTime(HttpContext context)
{
return "我是Substitution控件的内容,我没缓存 更新频率1秒:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
B、用Substitution 控件,以程序化API方式来实现。例如:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs"
Inherits="WebApplication.Index" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>
<!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">
<meta http-equiv="refresh" content="1;URL=Index.aspx" />
<title>使用 Substitution 控件建立缓存后替换</title>
</head>
<body>
<form id="form1" runat="server">
我是Substitution控件之外的内容,我缓存了 10秒:
<% Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); %><br />
我需要适时更新,不能缓存:<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
Substitution sub = new Substitution();
sub.MethodName = "GetTime";
this.PlaceHolder1.Controls.Add(sub);
}
/// <summary>
/// MethodName 刷新调用方法
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static String GetTime(HttpContext context)
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
C、用AdRotator控件,以AdRotator控件隐式实现。使用AdRotator控件实现缓存后替换很方便,主要原因是该控件本身具支持缓存后替换。将其 AdvertisementFile 属性指定 对应的广告 轮显 XML文件。例如:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="10" VaryByParam="None" %>
<!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>使用AdRotator控件实现缓存后替换</title>
<meta http-equiv="refresh" content="1;URL=Default.aspx" />
</head>
<body>
<form id="form1" runat="server">
我是AdRotator控件之外的内容,我缓存了 10秒:
<% Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); %><br />
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/App_Data/Ad.xml" />
</form>
</body>
</html>
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File">
<Ad>
<ImageUrl>~/images/ASP.NET编程词典.bmp</ImageUrl>
<NavigateUrl>http://www.mrbccd.com</NavigateUrl>
<AlternateText>编程词典网</AlternateText>
<Impressions>100</Impressions>
</Ad>
<Ad>
<ImageUrl>~/images/视频学.bmp</ImageUrl>
<NavigateUrl>http://www.mingribook.com</NavigateUrl>
<AlternateText>明日图书网</AlternateText>
<Impressions>50</Impressions>
</Ad>
<Ad>
<ImageUrl>~/images/典型模块.bmp</ImageUrl>
<NavigateUrl>http://www.mingribook.com</NavigateUrl>
<AlternateText>明日图书网</AlternateText>
<Impressions>50</Impressions>
</Ad>
</Advertisements>