【原创】页面级缓存简单使用教程

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;
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_WebUserControl : System.Web.UI.UserControl
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
this.LabelContentTime.Text = "缓存加载时间:" + DateTime.Now.ToString();
    }

}


.Net2.0提供了名为Substitution的新控件,该控件可使开发人员在缓存的web页面中插入动态内容。例如,开发人员可以显示终端用户的名称,该名称在一个缓存的,包含一些文字或者图片的web页面中动态生成。Substitution控件包含MethodName属性,调用该属性设置的方法可以返回动态内容。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Test_Default4" %>
<%@ 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>
    
<h4>.Net2.0提供了名为Substitution的新控件,该控件可使开发人员在缓存的web页面中插入动态内容。例如,开发人员可以显示终端用户的名称,该名称在一个缓存的,包含一些文字或者图片的web页面中动态生成。Substitution控件包含MethodName属性,调用该属性设置的方法可以返回动态内容。</h4>
        
<asp:Substitution ID="SubstitutionCache" runat="server" MethodName="GetDateTime" />
        
<br />
        
<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_Default4 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
this.LabelTime.Text = "页面加载时间:" + DateTime.Now.ToString();
    }


    
protected static string GetDateTime(HttpContext context)
    
{
        return "缓存加载时间:" + DateTime.Now.ToString();

    }

}

posted @ 2008-06-29 19:54  菜菜灰  阅读(2712)  评论(12编辑  收藏  举报