Implementing SQL Server 7/2000 Dependency Caching
You are now going to configure//配置 your Web application to use SQL Server 7/2000 dependency caching and then add a page that takes advantage of//利用 it.
The ASP.NET 2.0 web.config schema //图解,计划,模式has a new <caching> tag for configuring SQL Server 7/2000 dependency caching.
1. Open the web.config file. After the <system.web> <注意:不是放在</system.web>后面>element place the the following <caching> element:
<caching>
<sqlCacheDependency enabled="true" pollTime="5000">
<databases>
<add name="pubs" connectionStringName="pubsConnectionString1" />
</databases>
</sqlCacheDependency>
</caching>
The pollTime attribute //pooTime属性specifies how frequently //频率ASP.NET should check the cache table. The unit is milliseconds//毫秒, so in this case the polling occurs every five seconds. The connectionStringName is obvious//明显的, as it refers to the connection string already specified//指定的. The name "pubs" is the key that is referenced //参考in the OutputCache directive //输出缓存指令discussed below.
Before using dependency caching, create a new Web Form by copying the existing Authors.aspx page and making some slight//小的 modifications.
2. In the Solution Explorer, copy and paste Authors.aspx. Rename the resulting "Copy of Authors.aspx" file to "DependencyCaching.aspx". Right-click the new file and select View Code. Change the class name //类名from "Authors" to "DependencyCaching".
3. Open DependencyCaching.aspx. In the Page directive//页面指令, change //改变the value of the Inherits attribute //内在属性to "DependencyCaching". After the <div> tag add "<h2>Dependency Caching</h2>", and then change "Current Date/Time" to "Timestamp"//时间戳.
4. Add the following OutputCache directive//输出缓存指令 below//在...下面 the Page directive//指令: <%@ OutputCache Duration="3600" SqlDependency="pubs:authors" VaryByParam="none" %>.
The duration //持续<时间>attribute //属性specifies//指示,指明 that if the page does not get invalidated //不会失效by an underlying data change //改变within one hour, it will automatically invalidate//自行失效 itself. The SqlDependency attribute uses a "database name:table name" format //格式that maps to//映射 what was specified//指定的 in the web.config file. The following figure //图should clarify //澄清,阐明this relationship//关系:

5. Press F5 to run the application. Use the menu to run your SqlCaching.aspx <注:实际的页面名称是DependencyCaching.aspx:要在web.sitemap中加入<siteMapNode url="DependencyCaching.aspx" title="Sql Dependency Caching" description="" />才会有显示menu中,上第二篇文章《Page Output Caching》中有叙述>page. Refresh it a few times to see that the page is being cached (the date/time will not be changing because it is cached):

6. For any row, click Edit and make a change. Note the timestamp //时间戳and then click Update. If the timestamp //时间戳has not changed it means ASP.NET has not polled //测验,侦测SQL Server since the update (remember, the polling interval //测验时间间隔is set to 5 seconds). Wait a few seconds and then refresh the page again to see the new timestamp.
7. Close the browser.
浙公网安备 33010602011771号