数据库缓存机制配置
数据库缓存机制配置
看了老大苏鹏的webcast讲为sql缓存依赖项..自已动手做了一下为sql设置缓存
第一步:为 SQL 缓存依赖项启用该数据库。
打开Visual Studio2005命令提示,如图.
将进入如。并输入
aspnet_regsql.exe -S (local) -E -ed -d Northwind -et -t Employee
这里的-S为你的sql服务器
-E说明为集成window验证.
-et配置sql缓存依赖项
-d 是你将要被缓存的数据库,-t 缓存表
等等。
你可以用aspnet_regsql.exe -?查看.
配置成功后
将看到有如下一个表(AspNet_SqlCacheTablesForChangeNotification)在Northwind中
并且存在如下一些存储过程
上述配置完后.
第二步。建web网站
新建一个web网站。在web.config中输入如下:
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<connectionStrings>
<add name="conn" connectionString="server=.;database=Northwind;uid=sa;pwd='';" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="1000">
<databases>
<add name="Northwind" connectionStringName="conn" pollTime="1000"/>
</databases>
</sqlCacheDependency>
</caching>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="false" />
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Windows" />
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
在default.aspx页面中输入
<%@ OutputCache Duration="600" SqlDependency="Northwind:Employees" VaryByParam="none" %>
<!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 style="width:100px">
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1"
>
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>"
SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees]"></asp:SqlDataSource>
<br />
当前时间
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
在后台的cs中。
{
this.Label1.Text = System.DateTime.Now.ToString();
}
工作完成了,结果。当我们每次刷新,时间都不会变,因为缓存了页面。
------------------------------------------------------------------
------------------------------------------------------------------
当我们修改数据库中的Employees的字段后 .修改了第9条记录。。
------------------------------------------------------------------
------------------------------------------------------------------
发现了吗。
时间变了。。。当我们只要修改数据库后就发生了改变
示例下载