您的位置:首页 > 数据库

亲自动手测试PetShop4.0中所谓的"允许中间层对象缓存在后端数据库信息更改时自动失效"

2008-07-10 15:51 447 查看
微软的PetShop4.0的说明的链接中有这么一段话:ASP.NET2.0是SQL缓存依赖项:允许中间层对象缓存在后端数据库信息更改时自动失效。对于SQL2000,它在表级别有效(如同在PetShop4中一样),而对于SQLServer2005,它也可以在单独的行级别有效。利用该功能,缓存的数据库信息可以始终保持是最新的,同时仍利用缓存来降低中间层和数据库服务器上的负载。

其中重点是:允许中间层对象缓存在后端数据库信息更改时自动失效

我调试了一下PetShop4.0,调试时,直接在数据库里修改表product的Name的修息,看它有没有在主动刷新或跳转后再回来后程现的是最新的数据,我的结论是它有时侯行,有时侯不行,但总算比VS2003中好用.

由于PetShop4.0解决方案源代码是一整套,对于想单独了解此功能感觉太多东西了,所以我单独写一小例子,

现在我就来实例测一下:

1.建程序,命名WebApplication为TestImmediateCache,在Web.Config的<system.web>节点中加入

<caching>

<sqlCacheDependencyenabled="true"pollTime="10000">

<databases>

<addname="MSPetShop4"connectionStringName="SQLConnectionString"pollTime="10000"/>

</databases>

</sqlCacheDependency>

</caching>


上面的配置主要作用是将缓存与指定的DB持钩,在<configuration>节点中加入

<appSettings>

<addkey="SQLConnectionString"value="datasource=apj007;initialcatalog=MSPetShop4;userid=sa;password=1;persistsecurityinfo=true;packetsize=4096;minpoolsize=1;maxpoolsize=50;connectiontimeout=180"/>

</appSettings>

<connectionStrings>

<addname="SQLConnectionString"connectionString="datasource=apj007;initialcatalog=MSPetShop4;userid=sa;password=1;persistsecurityinfo=true;packetsize=4096;minpoolsize=1;maxpoolsize=50;connectiontimeout=180"/>

</connectionStrings>


2.添加页面代码

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="TestImmediateCache._Default"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>UntitledPage</title>

</head>

<body>

<formid="form1"runat="server">

<asp:ScriptManagerID="ScriptManager1"runat="server"/>

<div>

<table>

<tr>

<td>

<asp:ButtonID="Button1"runat="server"Text="Button"/></td>

<td>

</td>

<td>

</td>

</tr>

<tr>

<td>

<asp:LabelID="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#的:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.SqlClient;

usingSystem.Web.Caching;

namespaceTestImmediateCache

{

publicpartialclass_Default:System.Web.UI.Page

{

protectedAggregateCacheDependencydependency=newAggregateCacheDependency();

protectedvoidPage_Load(objectsender,EventArgse)

{

stringconStr=ConfigurationManager.AppSettings["SQLConnectionString"];

stringsqlStr="";

sqlStr="selectNamefromProductwhereProductID=@ProductID";//FI-08

SqlParameter[]parms;

parms=newSqlParameter[]

{

newSqlParameter("@ProductID",SqlDbType.VarChar,10)

};

parms[0].Value="FI-08";

//AddtoCache

stringdata=(string)HttpRuntime.Cache["PetName"];

if(data==null)

{

objectobj=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);

intcacheDuration=12;

dependency.Add(newSqlCacheDependency("MSPetShop4","Product"));

AggregateCacheDependencycd=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.UserControluc=newUserControl();

//uc.CachePolicy

}

}

}



最后调试后,发现情况还是和PetShop4.0一样,有时侯行,有时侯要刷一次以上才可以.那位大虾看到的话,解决问题可以留个答复给我吗?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐