SQL Express Dependency Caching
SQL Express dependency caching is easier to set up because SQL Express natively //天生地supports change notifications//通知. You do not need to run special command line utilities//命令行工具 to prepare the database because notifications are automatically generated //产生by SQL Express.
Moreover//而且,更多, the SqlCommand class //SQL命令类inherently //天生地,固有地supports dependency caching. Simply instantiate//实例化 the SqlCacheDependency class //SQL命令类with a SqlCommand object //SQL命令对像and then use the Response object's AddCacheDependency method to add the SqlCacheDependency object //对像to the page's response.
Data source objects// 数据源对像 also support SQL dependency caching. In this lesson you will set properties of the authorsObjectDataSource object <前面已经建立的authors的对像数据源对像>to enable SQL dependency caching.
1. In the Design view for DependencyCaching.aspx, select authorsObjectDataSource and press F4.
2. Near the top is a section for cache properties. Set CacheDuration//缓存持续 to "3600", EnableCaching to True and SqlCacheDependency to "pubs:authors".

Notice that these are essentially //本质上the same values as you set via the OutputCaching directive//页面缓存指令 in the previous lesson.
In this lesson, however, you are not caching the entire page output, but only the data retrieved// 取回,恢复by the object data source//对像数据源. As such, you need to make a few other modifications to the page.
3. Click Source and remove//移走,删除 the OutputCaching directive from the DependencyCaching.aspx.
4. Click Design and then double-click the ObjectDataSource control to add a handler //处理器for its Selecting event.
5. In the authorsObjectDataSource_Selecting event handler //事件处理器,事件处理函数add the following code, which sets the Label control's Text property to the time and date of the last execution //执行of the ObjectDataSource.Select method—in other words, the latest actual roundtrip //来回to the database:
| C# | VB | |
dateTimeLabel.Text = System.DateTime.Now.ToString();
Cache["LastRetrieval"] = System.DateTime.Now.ToString();
|
||
6. In the Page_Load event handler, replace the existing code with the following:
| C# | VB | |
if (Cache["LastRetrieval"] != null)
{
dateTimeLabel.Text = Cache["LastRetrieval"].ToString();
}
|
||
7. Press F5 to run the application. Refresh //刷新the page a few times to see that the timestamp//时间戳 remains unchanged. For any row, click Edit, make a change, and then click Update. Note the timestamp has changed. After making an update do not continue to press F5 to refresh to page<此时刷新timestamp时间是跟着改变的>. Instead, use the navigation menu //导般菜单to return to the page//回到那个页面<刷新了但timestamp时间是跟着改变的>. Otherwise //否则you will have postback issues arising from just having edited the GridView data<这句话没怎么懂>.
If at any time caching does not seem to work as expected, press ALT+Tab and stop the Web server for your application. Sometimes if you make changes with the development Web server still running//Web服务器正在运行时改变, the results are unexpected//不可预料的.
浙公网安备 33010602011771号