在Sql2005使用依赖缓存结合Ajax时时刷新页面

最近学习了《ASP.NET 2.0 开发指南》,书中对缓存做了详细的介绍。
大家都知道,利用缓存可以提高整个系统的性能。当时考虑到缓存的过期问题,.NET 2.0在1.1的基础上作出了改进。
下面看看依赖缓存在sql2000和sql2005中处理的方式有不同之处:
sql2000:基于轮询的失效. 此机制使用轮询检查表自从页被缓存以来是否已更新
sql2005:通知的缓存失效 此机制使用 Sql Server 2005 的查询更改通知机制来检测查询结果的更改。
详细的可以参照这个文章:SQL 缓存失效 2.0 版中的新增功能
1、在数据库查询分析器输入:ALTER DATABASE northwind SET ENABLE_BROKER  开启Broker服务
2、web.config配置数据库连接字符串:
    <connectionStrings>
        
<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
    
</connectionStrings>

3、在Global.asax文件的Application_StartApplication_End事件对调用
System.Data.SqlClient.SqlDependency.Start() 和System.Data.SqlClient.SqlDependency.Stop()进行SqlDependency的开启和关闭操作。
        protected void Application_Start(object sender, EventArgs e)
        
{
            
string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
            SqlDependency.Start(str);
        }


        
protected void Application_End(object sender, EventArgs e)
        
{
            
string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
            SqlDependency.Stop(str);
        }

4、显示页面的代码如下:
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="TestCache._Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace
="System.Web.UI" TagPrefix="asp" %>
<%OutputCache SqlDependency="CommandNotification" VaryByParam="none" Duration="5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:ScriptManager ID="ScriptManager1" runat="server">
            
</asp:ScriptManager>
            
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                
<ContentTemplate>
                    
<%= DateTime.Now.ToString() %>
                    
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
                    
</asp:GridView>
                    
<asp:Timer ID="Timer1" runat="server" Interval="5000">
                    
</asp:Timer>
                
</ContentTemplate>
                
<Triggers>
                    
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"></asp:AsyncPostBackTrigger>
                
</Triggers>
            
</asp:UpdatePanel>
            
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                SelectCommand
="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] ORDER BY [ProductID]">
            
</asp:SqlDataSource>
        
</div>
    
</form>
</body>
</html>

蓝色需要注意的是:
OutputCache SqlDependency="CommandNotification" :
基于通知的依赖项是使用字符串 CommandNotification 在 OutputCache 指令上 配置的。此值告知 ASP.NET 应为页或数据源控件创建基于通知的 依赖项。
Duration:设置缓存的过期时间(秒)。
asp:timer中的Interval设置页面刷新的时间。每次刷新还需要绑定数据控件,测试的时候我们在后台修改数据库某个行数据以后,页面不会得到更新的。所以需要在Page_Load加入GridView的绑定。
        protected void Page_Load(object sender, EventArgs e)
        
{
            
this.GridView1.DataBind();
    
        }



源码下载
posted @ 2008-06-26 09:42  [我是迭戈]  阅读(633)  评论(1)    收藏  举报