当前位置:编程学习 > asp >>

ASP.NET缓存 之 Web服务器缓存

1.替换缓存
  可以使用substitution动态更新缓存页
[csharp] 
public static string GetTime(HttpContext context) 

    return DateTime.Now.ToString(); 

aspx
[html]
Cached Time:<%=DateTime.Now.ToString() %> 
  <br />Page Time: 
      <asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime" /> 

最后在页面开启缓存输出
结果:Page Time每次都不相同,cached Time每5秒更新一次
Tips:关闭缓存输出
[csharp]
this.Response.Cache.SetNoServerCaching(); 
2.数据依赖
   依赖于数据库的页面,与其相应的sql语句关联,设置此缓存的方法:
[html]
<%@ Outputcache duration="5" varybyparam="none" SqlDependency="CommandNotification" %> 

一旦页面在缓存中,如果使用DML对数据源进行操作,她会发送通知给依赖的服务器,当服务器接到通知后,就会从缓存中移除原始查询页面.
如果页面中有数据源不需要缓存,可以绕过SqlDependency,对需要的查询语句使用AddCacheDependency代码如下:
[csharp]
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文件中添加如下代码:
[csharp] 
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对数据源做了更新,删除,插入操作,后台程
[html]
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="userId" %> 

序会收到一个通知,
从缓存中移除该页面,刷新页面,会看到最新的信息
3.使用varyByCustom对页进行缓存
我下面的代码使用查询字符串进行页缓存的,你可以使用Cookie,浏览器类型进行缓存.
[html] 
<div> 
  cache time:<%=DateTime.Now.ToString() %> 
  userId: 
  <%=userId %> 
  <br /> 
 
      <asp:GridView ID="GridView1" runat="server"> 
      </asp:GridView> 
  </div> 

查询字符串:userId
 
[html]
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="userId" %> 

 
[csharp] 
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 
              &

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,