微軟的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一樣,有時侯行,有時侯要刷一次以上纔可以. 那位大蝦看到的話,解決問題可以留個答覆給我嗎?