页面级缓存简单使用教程
上一篇 / 下一篇 2008-06-29 23:37:01 / 个人分类:转载
Duration属性是用来设置缓存的期限,单位为秒。缓存时间最长为5分钟。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Test_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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Duration属性是用来设置缓存的期限,单位为秒。缓存时间最长为5分钟。
<asp:Label ID="LabelTime" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Test_Default : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
this.LabelTime.Text = DateTime.Now.ToString();
}
}
VaryByParam属性是用来指定参数。如果将该属性设置为none,即表示该页面不会根据参数的不同来改变缓存。只要给该属性一个传入的参数,那么,根据参数的不同,就可以创建不同的缓存。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Test_Default2" %>
<%@ OutputCache Duration="10" VaryByParam="id" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>VaryByParam属性是用来指定参数。如果将该属性设置为none,即表示该页面不会根据参数的不同来改变缓存。只要给该属性一个传入的参数,那么,根据参数的不同,就可以创建不同的缓存。</h4>
<ul>
<li><a href="Default2.aspx">没有参数</a></li>
<li><a href="Default2.aspx?id=1">参数ID=1</a></li>
<li><a href="Default2.aspx?id=2">参数ID=2</a></li>
</ul>
<asp:Label ID="LabelTime" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Test_Default2 : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
string ID = Request.Params["id"];
if (ID == null)
{
this.LabelTime.Text = "默认的缓存";
}
else
{
if (ID == "1")
{
this.LabelTime.Text = "存储ID=1";
}
if (ID == "2")
{
this.LabelTime.Text = "存储ID=2";
}
}
this.LabelTime.Text += "<br />";
this.LabelTime.Text += DateTime.Now.ToString(); ;
}
}
使用页面分段缓存,在特殊情况下,只需要对页面的一部分进行缓存。如对于每位用户的登录欢迎信息可能会不同, 这部分内容就不能进行缓存,而公共浏览的信息部分就可能需要缓存。这就需要用到分段缓存的技术了。其实说白了,这也就是单独的用户控件的应用。即只对用户 控件部分缓存,而主页不启用缓存。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Test_Default3" %>
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>使
用页面分段缓存,在特殊情况下,只需要对页面的一部分进行缓存。如对于每位用户的登录欢迎信息可能会不同,这部分内容就不能进行缓存,而公共浏览的信息部
分就可能需要缓存。这就需要用到分段缓存的技术了。其实说白了,这也就是单独的用户控件的应用。即只对用户控件部分缓存,而主页不启用缓存。</h4>
<asp:Label ID="LabelDefaultTime" runat="server" Text="Label"></asp:Label>
<br />
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Test_Default3 : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
this.LabelDefaultTime.Text = "页面加载时间:" + DateTime.Now.ToString();
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Test_WebUserControl" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>
<asp:Label ID="LabelContentTime" runat="server" Text="Label"></asp:Label>
using System;
using System.Collections;导入论坛 引用链接 收藏 分享给好友 推荐到圈子 管理 举报
TAG: