1.替换缓存
可以使用substitution动态更新缓存页
public static string GetTime(HttpContext context) { return DateTime.Now.ToString(); }
aspxCached Time:<%=DateTime.Now.ToString() %> <br />Page Time: <asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime" />
最后在页面开启缓存输出结果:Page Time每次都不相同,cached Time每5秒更新一次
Tips:关闭缓存输出
this.Response.Cache.SetNoServerCaching();
2.数据依赖
依赖于数据库的页面,与其相应的sql语句关联,设置此缓存的方法:
<%@ Outputcache duration="5" varybyparam="none" SqlDependency="CommandNotification" %>
一旦页面在缓存中,如果使用DML对数据源进行操作,她会发送通知给依赖的服务器,当服务器接到通知后,就会从缓存中移除原始查询页面.如果页面中有数据源不需要缓存,可以绕过SqlDependency,对需要的查询语句使用AddCacheDependency代码如下:
using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = @"SELECT U.UserId, U.UserName, D.BorrowDate, D.BorrowMoney, D.RevertDate, D.IsRevert FROM dbo.UserInfo AS U INNER JOIN dbo.Debt AS D ON U.UserId = D.UserId"; conn.Open(); SqlCommand sqlCmd = new SqlCommand(sql,conn); SqlCacheDependency dep = new SqlCacheDependency(sqlCmd); GridView1.DataSource = sqlCmd.ExecuteReader(); GridView1.DataBind(); this.Response.AddCacheDependency(dep); }
注意此时我们还需要在Global.asax文件中添加如下代码:
void Application_Start(object sender, EventArgs e) { // Code that runs on application startup System.Data.SqlClient.SqlDependency.Start(ConfigurationManager.AppSettings["DebtDB"].ToString()); } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown System.Data.SqlClient.SqlDependency.Stop(ConfigurationManager.AppSettings["DebtDB"].ToString()); }
结果:执行当前页面请求时候,生成的页面放到输出缓存,如果使用DML对数据源做了更新,删除,插入操作,后台程
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="userId" %>
序会收到一个通知,从缓存中移除该页面,刷新页面,会看到最新的信息
3.使用varyByCustom对页进行缓存
我下面的代码使用查询字符串进行页缓存的,你可以使用Cookie,浏览器类型进行缓存.
<div> cache time:<%=DateTime.Now.ToString() %> userId: <%=userId %> <br /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div>
查询字符串:userId
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="userId" %>
protected string userId; public readonly string connectionString = ConfigurationManager.AppSettings["DebtDB"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["userId"] != null) { userId = Request.QueryString["userId"].ToString(); } using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = @" SELECT U.UserId, U.UserName, D.BorrowDate, D.BorrowMoney, D.RevertDate, D.IsRevert FROM dbo.UserInfo AS U INNER JOIN dbo.Debt AS D ON U.UserId = D.UserId WHERE D.UserId =" + userId; conn.Open(); SqlCommand sqlCmd = new SqlCommand(sql, conn); GridView1.DataSource = sqlCmd.ExecuteReader(); GridView1.DataBind(); } }
注意我们需要在Global.asax重写GetVaryByCustomString
如果不重写就不能保证根据UserId查询过滤出来对应的信息进行缓存。
例如我第一次 查询UserId:1的相关信息,然后再点击Userid:2的相关信息,居然一模一样,只有等待缓存过期才会显示对应userid:2的信息
public override string GetVaryByCustomString(HttpContext context, string custom) { if (custom == "userId") { return context.Request.QueryString["userId"].ToString(); } return base.GetVaryByCustomString(context, custom); }