系列文章導航:
溫故知新ASP.NET 2.0(C#)(1) - MasterPage(母版頁)
溫故知新ASP.NET 2.0(C#)(1) - MasterPage(母版頁)
溫故知新ASP.NET 2.0(C#)(2) - Themes(主題)
溫故知新ASP.NET 2.0(C#)(3) - SiteMap(站點地圖)
溫故知新ASP.NET 2.0(C#)(4) - 緩存&SqlCacheDependency
溫故知新ASP.NET 2.0(C#)(5) - Localization
溫故知新ASP.NET 2.0(C#)(8) - DataSourceControl
溫故知新ASP.NET 2.0(C#)(6) - Membership&RoleManager
溫故知新ASP.NET 2.0(C#)(7) - Profile
溫故知新ASP.NET 2.0(C#)(8) - DataSourceControl
介紹
緩存是在計算中廣泛使用的一種技術,通過將經常訪問的數據或存取開銷較大的數據保留在內存或硬盤中來提高性能。在 Web 應用程序的上下文中,緩存用于在 HTTP 請求之間保留頁或數據,在重用它們時可以不必耗費資源重新創建。
關鍵
1、@OutputCache指令中的屬性:
Duration - 緩存時間(秒)
VaryByParam - 根據使用 POST 或 GET 發送的名稱/值對來改變緩存的結果(多參數用分號隔開)
VaryByControl - 根據用戶控件中的控件來改變緩存的片段(值是控件ID,多控件用分號隔開)
CacheProfile - 調用配置文件中設置的緩存時間
2、增加數據緩存時用Cache.Insert,可以指定緩存時間
3、替換緩存(Substitution)- 回調函數要是靜態的
4、SqlCacheDependency
配置文件中的配置

Code
<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="輪詢時間(毫秒)">
<databases>
<add name="名字" connectionStringName="連接字符串的名字" />
</databases>
</sqlCacheDependency>
<!-- 如果是SqlServer2005的話,則只需如下設置,因為SqlServer支持基于通知的緩存失效
<sqlCacheDependency enabled="true" />
-->
</caching>
</system.web>
如果不是SqlServer2005的話,應該使用aspnet_regsql注冊一下
aspnet_regsql.exe -S "server" -E -d "database" -ed
aspnet_regsql.exe -S "server" -E -d "database" -et -t "table"
如果是Sql驗證的話要把-E換成,-U(用戶名),-P(密碼)
示例
頁面輸出緩存
Cahce/Page.aspx

Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Page.aspx.cs"
Inherits="Cahce_Page" Title="頁面輸出緩存" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
Duration="10" VaryByParam="none"
</p>
<p>
<%=DateTime.Now %>
</p>
</asp:Content>
API操作緩存
Cahce/Page.aspx.cs

Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Cahce_Page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/**//* 通過API設置緩存 不常用啊
// 相當于@OutputCache指令中的Duration屬性
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
// 以指定響應能由客戶端和共享(代理)緩存進行緩存。
Response.Cache.SetCacheability(HttpCacheability.Public);
// 過期時間可調
Response.Cache.SetSlidingExpiration(true);
*/
}
}
頁面輸出緩存(VaryByParam)
Cahce/PageByParam.aspx

Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PageByParam.aspx.cs"
Inherits="Cahce_PageByParam" Title="頁面輸出緩存(VaryByParam)" %>
<%@ OutputCache CacheProfile="CacheTest" VaryByParam="p1;p2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
CacheProfile="CacheTest" VaryByParam="p1;p2"
<br />
CacheProfile="CacheTest" - 從web.config中讀信息
<br />
get或post方式都行
</p>
<p>
<a href="?p1=a&p2=b">第一組</a>
<br />
<a href="?p1=c&p2=d">第二組</a>
<br />
<a href="?p1=e&p2=f">第三組</a>
</p>
<p>
<%=DateTime.Now %>
</p>
</asp:Content>
上面頁所需的web.config中的配置

Code
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheTest" duration="10" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
頁面輸出緩存(VaryByControl)
Cahce/CacheControl.ascx

Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CacheControl.ascx.cs"
Inherits="Cahce_CacheControl" %>
<%@ OutputCache Duration="10" VaryByControl="DropDownList1" %>
<p>
<%=DateTime.Now %>
</p>
<p>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="XmlDataSource1"
DataTextField="text" DataValueField="value">
</asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Config/DropDownListData.xml">
</asp:XmlDataSource>
</p>
<p>
<asp:Button ID="btn" runat="Server" Text="提交" />
</p>
Cahce/PageByControl.aspx

Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PageByControl.aspx.cs"
Inherits="Cahce_PageByControl" Title="頁面輸出緩存(VaryByControl)" %>
<%@ Register Src="CacheControl.ascx" TagName="CacheControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
未經緩存設置的容器頁:
<%=DateTime.Now %>
</p>
<p>
經過VaryByControl設置的用戶控件,根據DropDownList的不同緩存不同的內容(用戶控件中的@OutputCache指令為Duration="10"
VaryByControl="DropDownList1"):<br />
<uc1:CacheControl ID="CacheControl1" runat="server" />
</p>
</asp:Content>