系列文章导航:
温故知新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
数据缓存
Cahce/Data.aspx
Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Data.aspx.cs"
Inherits="Cahce_Data" Title="数据缓存" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
<asp:Label ID="lbl" runat="server" />
</p>
</asp:Content>
Cahce/Data.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_Data : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 一看就懂
if (Cache["key"] == null)
{
Cache.Insert("key", DateTime.Now, null, DateTime.Now.AddSeconds(10), TimeSpan.Zero);
}
DateTime dt = (DateTime)Cache["key"];
lbl.Text = dt.ToString();
}
}
替换缓存(部分区域强行不使用缓存)
Cahce/Substitution.aspx
Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Substitution.aspx.cs"
Inherits="Cahce_Substitution" Title="替换缓存(部分区域强行不使用缓存)" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
Duration="10" VaryByParam="none"
</p>
<p>
<%=DateTime.Now %>
</p>
<p>
API 向返回当前日期的静态方法中插入动态回调,此回调在每次请求时都会执行<br />
<% Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetTime)); %>
</p>
<p>
使用Substitution 控件插入动态内容<br />
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime" />
</p>
</asp:Content>
Cahce/Substitution.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_Substitution : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// 回调函数所调的静态方法
public static string GetTime(HttpContext context)
{
return DateTime.Now.ToString();
}
}
SqlCacheDependency页的Sql缓存
Cahce/SqlCachePage.aspx
Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SqlCachePage.aspx.cs"
Inherits="Cahce_SqlCachePage" Title="页的Sql缓存" %>
<%@ OutputCache Duration="999999" SqlDependency="VS2005_Test:sqlcache" VaryByParam="none" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
Duration="999999" SqlDependency="VS2005_Test:sqlcache" VaryByParam="none"<br />
如果是SqlServer2005则改成SqlDependency="CommandNotification<br />
注意配置文件中的配置
</p>
<p>
<%=DateTime.Now %>
</p>
</asp:Content>
数据源控件的Sql缓存
Cahce/SqlCachePage.aspx
Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SqlCacheDataSourceControl.aspx.cs"
Inherits="Cahce_SqlCacheDataSourceControl" Title="数据源控件的Sql缓存" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
DataSource控件设置如下属性:EnableCaching="True" SqlCacheDependency="VS2005_Test:sqlcache"
CacheDuration="Infinite"<br />
如果是SqlServer2005则改成SqlDependency="CommandNotification<br />
注意配置文件中的配置
</p>
<p>
<%=DateTime.Now %>
</p>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" EnableCaching="True" SqlCacheDependency="VS2005_Test:sqlcache"
CacheDuration="Infinite" ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>"
SelectCommand="SELECT * FROM [SqlCache]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="True">
</asp:GridView>
</p>
</asp:Content>
web.config中的相关配置
Code
<connectionStrings>
<add name="SqlConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VS2005_Test.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add name="VS2005_Test" connectionStringName="SqlConnectionString" />
</databases>
</sqlCacheDependency>
<!-- 如果是SqlServer2005的话,则只需如下设置,因为SqlServer支持基于通知的缓存失效
<sqlCacheDependency enabled="true" />
-->
</caching>
</system.web>
注意
Sql Server 2005
基于通知的缓存失效,不用aspnet_regsql设置,要设置属性SqlDependency="CommandNotification"。在首次执行某 SQL
查询之前,必须在应用程序某处调用 System.Data.SqlClient.SqlDependency.Start() 方法。此方法应放在
global.asax 文件的 Application_Start() 事件中。因为Sql Server 2005
基于通知的缓存失效对支持查询通知的查询语法有许多限制,所以我觉得最好先别用,而是使用轮询机制。在使用轮询机制时如本例子中的SqlCacheDependency="VS2005_Test:sqlcache",冒号前面是配置文件中配置的相关值指向数据库连接,后面是启用SqlCache的表名,注意区分大小写。