更多的时候,我们的服务器性能损耗还是在查询数据库的时候,所以对数据库的缓存还是显得特别重要,上面几种方式都可以实现部分数据缓存功能。但问题是我们的数据有时候是在变化的,这样用户可能在缓存期间查询的数据就是老的数据,从而导致数据的不一致。那有没有办法做到,数据如果不变化,用户就一直从缓存中取数据,一旦数据变化,系统能自动更新缓存中的数据,从而让用户得到更好的用户体验。
答案是肯定的!.NET已经为我们提供了这样一种非常好的解决方法:SqlCacheDependency数据库缓存依赖。
实现步骤:
下面就让我们看一下如何实现数据库缓存依赖功能:
第一步: 修改web.config,让项目启用SqlCacheDependency 。
将下列代码加入web.config的<system.web>节:
1 |
<div style="margin:0px;padding:0px;line-height:21px;"><span style="margin:0px;padding:0px;color:#0000ff;"><?</span><span style="margin:0px;padding:0px;color:#ff00ff;">xml version="1.0"</span><span style="margin:0px;padding:0px;color:#0000ff;">?></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">configuration</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">appSettings</span><span style="margin:0px;padding:0px;color:#0000ff;">/></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">connectionStrings</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">add </span><span style="margin:0px;padding:0px;color:#ff0000;">name</span><span style="margin:0px;padding:0px;color:#0000ff;">="strcodematic"</span><span style="margin:0px;padding:0px;color:#ff0000;"> connectionString</span><span style="margin:0px;padding:0px;color:#0000ff;">="data source=127.0.0.1;initial catalog=codematic;user id=sa;password="</span><span style="margin:0px;padding:0px;color:#ff0000;"> providerName</span><span style="margin:0px;padding:0px;color:#0000ff;">="System.Data.SqlClient"</span><span style="margin:0px;padding:0px;color:#ff0000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">/></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">connectionStrings</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">system.web</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">caching</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">sqlCacheDependency </span><span style="margin:0px;padding:0px;color:#ff0000;">enabled</span><span style="margin:0px;padding:0px;color:#0000ff;">="true"</span><span style="margin:0px;padding:0px;color:#ff0000;"> pollTime</span><span style="margin:0px;padding:0px;color:#0000ff;">="6000"</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">databases</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /> </span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">add </span><span style="margin:0px;padding:0px;color:#ff0000;">name</span><span style="margin:0px;padding:0px;color:#0000ff;">="codematic"</span><span style="margin:0px;padding:0px;color:#ff0000;"> connectionStringName</span><span style="margin:0px;padding:0px;color:#0000ff;">="strcodematic"</span><span style="margin:0px;padding:0px;color:#ff0000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">/></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">databases</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">sqlCacheDependency</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">caching</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">compilation </span><span style="margin:0px;padding:0px;color:#ff0000;">debug</span><span style="margin:0px;padding:0px;color:#0000ff;">="true"</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">compilation</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"><</span><span style="margin:0px;padding:0px;color:#800000;">authentication </span><span style="margin:0px;padding:0px;color:#ff0000;">mode</span><span style="margin:0px;padding:0px;color:#0000ff;">="Windows"</span><span style="margin:0px;padding:0px;color:#0000ff;">/></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">system.web</span><span style="margin:0px;padding:0px;color:#0000ff;">></span><span style="margin:0px;padding:0px;color:#000000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;"></</span><span style="margin:0px;padding:0px;color:#800000;">configuration</span><span style="margin:0px;padding:0px;color:#0000ff;">></span></div> |
这里的connectionStringName指定了在<connectionStrings>中添加的某一个连接字符串。name则是为该SqlCacheDependency起的名字,这个名字将在第3步中用到。
SqlCacheDependency类会自动完成对此配置节信息的读取以建立和数据库之间的联系。
注意:在<databases>节的<add name="codematic" connectionStringName="strcodematic"/>中的name属性值必须和第三步的Page_Load代码中System.Web.Caching.SqlCacheDependency("codematic","P_Product"); 中的第一个参数(数据库名称)相一致。
第二步:执行下述命令,为 数据库启用缓存依赖。
如果要配置SqlCacheDependency,则需要以命令行的方式执行。
aspnet_regsql.exe工具位于Windows\\Microsoft.NET\\Framework\\[版本]文件夹中。
aspnet_regsql -C "data source=127.0.0.1;initial catalog=codematic;user id=sa;password=" -ed -et -t "P_Product"
参数-C后面的字符串是连接字符串(请替换成自己所需要的值),
参数-t后面的字符串是数据表的名字。
运行结果如图15-3所示:
图15-3 启用数据库缓存依赖
命令执行后,在指定的数据库中会多出一个AspNet_SqlCacheTablesForChangeNotification表。
注意:
要使得7.0或者2000版本以上的SQLServer支持SqlCacheDependency特性,需要对数据库服务器执行相关的配置。
有两种方法配置SQLServer:
一使用aspnet_regsql命令行工具,
二使用SqlCacheDependencyAdmin类。
例如:
aspnet_regsql -S "server"-E -d "database"–ed 或者
aspnet_regsql -S "server"-E -d "database"-et -t "table"
如果是Sql验证的话要把-E换成,-U (用户名),-P (密码)
以下是该工具的命令参数说明:
-? 显示该工具的帮助功能;
-S 后接的参数为数据库服务器的名称或者IP地址;
-U 后接的参数为数据库的登陆用户名;
-P 后接的参数为数据库的登陆密码;
-E 使用当前登录用户的 Windows 集成认证进行身份验证。
-d 后接参数为对哪一个数据库采用SqlCacheDependency功能;
-C 连接数据库的连接字符串。如果您指定服务器(-S)和登录(-U和-P,或 -E)信息,则此选项不是必需的,因为连接字符串已经包含这些信息。
-t 后接参数为对哪一个表采用SqlCacheDependency功能;
-ed 允许对数据库使用SqlCacheDependency功能;
-dd 禁止对数据库采用SqlCacheDependency功能;
-et 允许对数据表采用SqlCacheDependency功能;
-dt 禁止对数据表采用SqlCacheDependency功能;
-lt 列出当前数据库中有哪些表已经采用sqlcachedependency功能。
第三步:在代码中使用缓存,并为其设置SqlCacheDependency依赖:
1 |
<div style="margin:0px;padding:0px;line-height:21px;"><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"> 获取当前应用程序指定CacheKey的Cache对象值<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"></summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="CacheKey"></span><span style="margin:0px;padding:0px;color:#008000;">索引键值</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><returns></span><span style="margin:0px;padding:0px;color:#008000;">返回缓存对象</span><span style="margin:0px;padding:0px;color:#808080;"></returns></span><span style="margin:0px;padding:0px;color:#808080;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">public</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">static</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> GetCache(</span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> CacheKey)<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /> System.Web.Caching.Cache objCache </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> HttpRuntime.Cache;<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">return</span><span style="margin:0px;padding:0px;color:#000000;"> objCache[CacheKey];<br style="margin:0px;padding:0px;line-height:10px;" />}<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"> 设置以缓存依赖的方式缓存数据<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"></summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="CacheKey"></span><span style="margin:0px;padding:0px;color:#008000;">索引键值</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="objObject"></span><span style="margin:0px;padding:0px;color:#008000;">缓存对象</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="cacheDepen"></span><span style="margin:0px;padding:0px;color:#008000;">依赖对象</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#808080;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">public</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">static</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">void</span><span style="margin:0px;padding:0px;color:#000000;"> SetCache(</span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> CacheKey, </span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> objObject, System.Web.Caching.CacheDependency dep)<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /> System.Web.Caching.Cache objCache </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> HttpRuntime.Cache;<br style="margin:0px;padding:0px;line-height:10px;" /> objCache.Insert(<br style="margin:0px;padding:0px;line-height:10px;" /> CacheKey,<br style="margin:0px;padding:0px;line-height:10px;" /> objObject,<br style="margin:0px;padding:0px;line-height:10px;" /> dep,<br style="margin:0px;padding:0px;line-height:10px;" /> System.Web.Caching.Cache.NoAbsoluteExpiration,</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">从不过期</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.Cache.NoSlidingExpiration,</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">禁用可调过期</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.CacheItemPriority.Default,<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">null</span><span style="margin:0px;padding:0px;color:#000000;">);<br style="margin:0px;padding:0px;line-height:10px;" />}<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">protected</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">void</span><span style="margin:0px;padding:0px;color:#000000;"> Page_Load(</span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> sender, EventArgs e)<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> CacheKey </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">cachetest</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">;<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> objModel </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> GetCache(CacheKey);</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">从缓存中获取</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">if</span><span style="margin:0px;padding:0px;color:#000000;"> (objModel </span><span style="margin:0px;padding:0px;color:#000000;">==</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">null</span><span style="margin:0px;padding:0px;color:#000000;">)</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">缓存里没有</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> {<br style="margin:0px;padding:0px;line-height:10px;" /> objModel </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> GetData();</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">把当前时间进行缓存</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">if</span><span style="margin:0px;padding:0px;color:#000000;"> (objModel </span><span style="margin:0px;padding:0px;color:#000000;">!=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">null</span><span style="margin:0px;padding:0px;color:#000000;">)<br style="margin:0px;padding:0px;line-height:10px;" /> {<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">依赖数据库codematic中的P_Product表变化 来更新缓存</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.SqlCacheDependency dep </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">new</span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.SqlCacheDependency(</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">codematic</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">, </span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">P_Product</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">);<br style="margin:0px;padding:0px;line-height:10px;" /> SetCache(CacheKey, objModel, dep);</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">写入缓存</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> }<br style="margin:0px;padding:0px;line-height:10px;" /> }<br style="margin:0px;padding:0px;line-height:10px;" /><br style="margin:0px;padding:0px;line-height:10px;" /> GridView1.DataSource </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> (DataSet)objModel;<br style="margin:0px;padding:0px;line-height:10px;" /> GridView1.DataBind();<br style="margin:0px;padding:0px;line-height:10px;" />}<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">查询数据</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">private</span><span style="margin:0px;padding:0px;color:#000000;"> DataSet GetData()<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> conString </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">data source=127.0.0.1;initial catalog=codematic;user id=sa;password=</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">;<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> strSQL </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">SELECT * FROM P_Product</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">;<br style="margin:0px;padding:0px;line-height:10px;" /> SqlConnection myConnection </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">new</span><span style="margin:0px;padding:0px;color:#000000;"> SqlConnection(conString);<br style="margin:0px;padding:0px;line-height:10px;" /> DataSet ds </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">new</span><span style="margin:0px;padding:0px;color:#000000;"> DataSet();<br style="margin:0px;padding:0px;line-height:10px;" /> myConnection.Open();<br style="margin:0px;padding:0px;line-height:10px;" /> SqlDataAdapter adapter </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">new</span><span style="margin:0px;padding:0px;color:#000000;"> SqlDataAdapter(strSQL, myConnection);<br style="margin:0px;padding:0px;line-height:10px;" /> adapter.Fill(ds, </span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">Product</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">);<br style="margin:0px;padding:0px;line-height:10px;" /> myConnection.Close();<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">return</span><span style="margin:0px;padding:0px;color:#000000;"> ds;<br style="margin:0px;padding:0px;line-height:10px;" />}</span></div> |
从以上代码可以看出,和文件依赖基本相同,只是在存放缓存SetCache时存入的依赖对象不同罢了。这里用的是SqlCacheDependency。
其中,创建SqlCacheDependency的构造方法:
public SqlCacheDependency(string databaseEntryName,string tableName)
databaseEntryName :是在Web.config 文件的 caching 节的 sqlCacheDependency 的databases 元素中定义的数据库的名称。
tableName :与 SqlCacheDependency 关联的数据库表的名称。
这样,只有当P_Product表的内容发生变化时,查询操作才会重新查询数据更新缓存的内容,可以大大减少数据库的重复查询和提高系统的性能和运行效率。