微软的PetShop4.0的说明的链接中有这么一段话: ASP.NET 2.0 是 SQL 缓存依赖项:允许中间层对象缓存在后端数据库信息更改时自动失效。对于 SQL 2000,它在表级别有效(如同在 Pet Shop 4 中一样),而对于 SQL Server 2005,它也可以在单独的行级别有效。利用该功能,缓存的数据库信息可以始终保持是最新的,同时仍利用缓存来降低中间层和数据库服务器上的负载。
其中重点是: 允许中间层对象缓存在后端数据库信息更改时自动失效
我调试了一下PetShop4.0,调试时,直接在数据库里修改表product的Name的修息,看它有没有在主动刷新或跳转后再回来后程现的是最新的数据, 我的结论是它有时侯行,有时侯不行,但总算比VS2003中好用.
由于PetShop4.0解决方案源代码是一整套,对于想单独了解此功能感觉太多东西了,所以我单独写一小例子,
现在我就来实例测一下:
1. 建程序, 命名WebApplication为TestImmediateCache, 在Web.Config的<system.web>节点中加入
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add name="MSPetShop4" connectionStringName="SQLConnectionString" pollTime="10000"/>
</databases>
</sqlCacheDependency>
</caching>
上面的配置主要作用是将缓存与指定的DB持钩, 在<configuration>节点中加入
<appSettings>
<add key="SQLConnectionString" value="data source=apj007;initial catalog=MSPetShop4;user id=sa;password=1;persist security info=true;packet size=4096;min pool size=1;max pool size=50;connection timeout=180"/>
</appSettings>
<connectionStrings>
<add name="SQLConnectionString" connectionString="data source=apj007;initial catalog=MSPetShop4;user id=sa;password=1;persist security info=true;packet size=4096;min pool size=1;max pool size=50;connection timeout=180"/>
</connectionStrings>
2. 添加页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestImmediateCache._Default" %> <!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>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div> <table> <tr> <td> <asp:Button ID="Button1" runat="server" Text="Button" /></td> <td> </td> <td> </td> </tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> </div> </form> </body> </html>C#的: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; using System.Data.SqlClient; using System.Web.Caching; namespace TestImmediateCache { public partial class _Default : System.Web.UI.Page { protected AggregateCacheDependency dependency = new AggregateCacheDependency(); protected void Page_Load(object sender, EventArgs e) { string conStr = ConfigurationManager.AppSettings["SQLConnectionString"]; string sqlStr = ""; sqlStr = "select Name from Product where ProductID=@ProductID"; //FI-08 SqlParameter[] parms; parms = new SqlParameter[] { new SqlParameter("@ProductID", SqlDbType.VarChar, 10) }; parms[0].Value = "FI-08"; //Add to Cache string data = (string)HttpRuntime.Cache["PetName"]; if (data == null) { object obj = DataAccess.SqlHelper.ExecuteScalar(conStr, CommandType.Text, sqlStr, parms); data = obj.ToString(); //HttpRuntime.Cache.Add("PetName", data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null); int cacheDuration = 12; dependency.Add(new SqlCacheDependency("MSPetShop4", "Product")); AggregateCacheDependency cd = this.dependency; HttpRuntime.Cache.Add("PetName", data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null); } this.Label1.Text = data; //this.CachePolicy.Dependency = this.dependency; //System.Web.UI.UserControl uc = new UserControl(); //uc.CachePolicy } } }
最后调试后,发现情况还是和PetShop4.0一样,有时侯行,有时侯要刷一次以上才可以. 那位大虾看到的话,解决问题可以留个答复给我吗?
本文通过一个简单的ASP.NET 2.0应用程序演示了SQL缓存依赖项的功能,该功能使得中间层对象能在后端数据库信息更新时自动失效,以确保缓存数据最新且减轻服务器负担。
207





