缓存:
1 全部缓存:缓存整个页面
<%@ OutputCache Duration="10" VaryByParam="none" %>
<%@ OutputCache Duration="10" VaryByParam="sex;name" %>
Duration:缓存10秒
VaryByParam:none 不随任可参数而变化 *根据的有参数变化
2 局部缓存:
(1)控件缓存
自定义控件带有缓存功能
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<%@ OutputCache Duration="2" VaryByParam="none" %>
<asp:Label ID="lblTime" runat="server" ></asp:Label>
或
ASP页面
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<%@ OutputCache Duration="3" VaryByControl="WebUserControl1" %>
控件定义
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Label ID="lblTime" runat="server" ></asp:Label>
(2)替换缓存
<asp:Label ID="lblTime" runat="server" />
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime"/>
public static string GetTime(HttpContext context)
{
return "<div style='color:red'>"+DateTime.Now.ToString()+"</div>";
}
此方法必须是静态,参数必须是HttpContext
3 数据缓存
Cache.Insert(key,value)
Cache.Add(key,value)
ASP.NET 缓存
最新推荐文章于 2025-04-12 16:15:34 发布