The sqldatasource control also sets the enablecaching attribute to true to automatically buffer data retrieved by selectcommand. This cacheduration attribute allows you to specify how long the data should be buffered before it is refreshed from the database (in seconds. By default, sqldatasource uses an absolute expiration Policy to cache data. This means that data is refreshed every second specified in the cacheduration attribute.
You can also choose to configure a "slide" Expiration Policy for sqldatasource. With this policy, data will not be deleted as long as it continues to be accessed. When you have a large number of items to be buffered, it is very useful to use the sliding expiration policy, because this policy allows you to keep only the items that are most frequently accessed in the memory. In the preceding example, set the enablecaching and cacheduration attributes to true and 10 respectively, and set the time period for buffering SQL query results to 10 seconds.
The SQL cache in the sqldatasource control is invalid.
So far, you have seen how to invalidate the cache based on the duration value set in the cacheduration attribute in the sqldatasource control. In this section, I will explain how to implement an invalid cache mechanism based on data in the SQL Server table. This cache invalidation mechanism is a new feature of ASP. NET 2.0. In this case, a buffer page or a data source control can be bound to a specific table in an SQL Server database. Once you implement this initialization Association operation, changing the table content will automatically invalidate the buffered object.
[Suggestion] When you need to use the same database data on multiple pages, you should consider using the SQL cache invalidation mechanism of the data source control.
In fact, the cache invalidation mechanism based on SQL Server applies to SQL Server 7.0 and later versions. However, SQL Server 7.0 and 2000 only support table-level cache invalidation mechanisms. This means that when data changes in any timetable, the buffered items are automatically invalid. SQL Server 2005 also provides an invalid row-level cache mechanism, which can control data buffering at a finer level.
In SQL Server 7 and SQL Server 2000, invalid table-level cache is supported by a query system (a specific ASP. NET process. The process queries the database ("pull" Model) at a specific time to monitor which tables have changed since the last check. Although this PULL model can be used in most cases, it is not a very effective method. Fortunately, this situation is greatly enhanced in SQL Server 2005-SQL Server is responsible for notifying (pushing model) ASP. NET whenever a specific data row is modified. SQL Server 2005 implements this function by using a feature called notification delivery services, which uses port 80). This service can be directly connected to HTTP of iis6.0. sys interacts to notify the Web server to update specific rows. Next, I will explain how to configure buffering for SQL Server 7 and 2000.
Before you can use SQL Server 7 or SQL Server 2000 to establish cache dependencies, you need to complete the following steps:
◆ Configuring SQL Server to support SQL cache is invalid. You only need to create tables or databases in the SQL Server database that you want to monitor at one time.
◆ Add necessary configuration information to the Web. config file.
Next, let's analyze the above steps in detail. First, let's take a look at the configuration of SQL Server.
4. configuring SQL Server to support SQL cache is invalid
You can configure SQL Server 2000 in the following two ways to support invalid SQL cache:
1. Use the aspnet_regsql tool;
2. Use the enabletableforconfigurications method of the sqlcachedependencyadmin class.
In this article, we only consider the first method. The above aspnet_regsql tool is responsible for creating another table aspnet_sqlcachetablesforchangenotification to track changes to all monitored tables in the database. It also creates a set of triggers and stored procedures to support this capability. To run the aspnet_regsql tool, you only need to open the Visual Studio command prompt line and enter a command similar to the following:
D:/>aspnet_regsql –S localhost –U sa –P thiru –d Pubs –ed
|
It should be shown as follows:
Enabling the database for SQL cache dependency. … Finished. D:/>
|
This command makes the pubs database support the SQL cache invalid function. Where:
◆ S-server name
◆ U-User ID used to connect to SQL Server
◆ P-user password used to connect to SQL Server
◆ D-Database Name
◆ Ed-The database supports the SQL Server-triggered invalid cache function.
Once the above operations are completed at the database level, you need to enable the invalid cache function at the single table level. For specific implementation, see the following commands:
D:/>aspnet_regsql –S localhost –U sa –P thiru –t authors –d Pubs –et
|
It should be shown as follows:
Enabling the table for SQL cache dependency. … Finished. D:/>
|
In the preceding command:
◆ T-specifies the table name
◆ Et-This table is supported and has an invalid cache function triggered by SQL Server.
The preceding command shows how to enable invalid SQL cache support for the authors table in the pubs database. Once you have configured the authors table to send notifications, ASP. NET will be notified when the data in the table changes at any time to invalidate the corresponding items in the cache.