Asp.net| Cache | Data in asp.net 2.0, new additions to many new features and controls. Among them, in the data caching function, than ASP.net 1.1 has a great change. As we all know, the caching function of data is very important, we can put some data that does not change for a period of time in the cache, so that we do not have to read the database every time, when the next time we need this data, can be directly from the cache, greatly enhance the efficiency.
In ASP.net 1.1, the caching function of data has been better implemented, but there is a small problem, that is, if the data in the database has changed, the cache cannot be updated within the specified time, but must wait for the cache to fail. For example, if you use page caching for some of the details of a product, such as prices, in a Web page, if you modify the information in the background, the user will not see the information immediately, and delay some time to see it. In some applications, if you want to do any updates to the database, you can immediately in the cache changes in effect, in ASP.net 1.1 is more difficult to implement, and in ASP.net 2.0, you can easily implement this feature. This article will introduce the implementation of this feature in the. NET Framework 2.0 beta,visual Web Developer Express Beta.
First, in ASP.net 2.0, new caching features are provided for SQL Server 7/2000, and for the upcoming SQL Server (Yukon). This highlights the caching capabilities for SQL Server 7/2000. This new data caching feature, called SqlCacheDependency, requires the following steps to implement this function:
1, under the. NET Framework 2.0 installation directory
(default is winnt\microsoft.net\framework\v2.0.40607), start a command line tool called Aspnet_regsql.exe, such as:
Aspnet_regsql-s localhost–u sa–p 123456-d pubs–ed
The above means that the local database server localhost is specified, the login username and password are specified, and the parameter-d specifies which database to use (here is the pubs specified) database,- The ed parameter indicates that the database is allowed to use the SqlCacheDependency feature.
Next, we need to specify which table to use the data SqlCacheDependency function, such as:
The above statement means that the SqlCacheDependency feature is specified for the authors table in the pubs database, with the-t argument followed by the name of the table,-et to allow the table to use the SqlCacheDependency feature. Other relevant parameters are briefly described below:
Parameter parameter meaning-? Displays the Help functionality for the tool.-S followed by the name of the database server or the IP address-u followed by the user name of the database.-P The following parameter is the login password for the database-e when using Windows Integrated authentication, use this feature-t The back-connect parameter is to which table the SqlCacheDependency function is used. The-D post-connect parameter is to which database uses SqlCacheDependency function-ed allows the use of the database SqlCacheDependency function-DD prohibit the use of SqlCacheDependency function to the database-et Allow the use of SqlCacheDependency functions for datasheets-DT prohibit the use of SqlCacheDependency functions on datasheets-lt list which tables in the current database have been used SqlCacheDependency features
SqlCacheDependency the principle of simple, is the use of aspnet_regsql this special tool, pre-set to monitor the database and tables, and add a special table in the database, open the database, you will find more than one datasheet aspnet_ Sqlcachetablesforchangenotification, as shown below:
This table has three fields, "TableName" records the name of the datasheet to be tracked, the time when the "notificationcreated" record starts tracking, "Changeid" is a field of type int, and whenever the data in the tracked datasheet changes, The value of this field is added by 1. In addition, several stored procedures are added to the specified database to allow the ASP.net engine to query the tracking data table and to add a number of triggers to the table to use the SqlCacheDependency, corresponding to the insert, Update, and delete operations respectively. Asp. NET engine by executing the stored procedure "Aspnet_sqlcachepollingstoredprocedure" It adds, this stored procedure returns directly to the "AspNet_SqlCacheTablesForChangeNotification "The contents of the table let the ASP.net engine know which table's data has changed.
Next, we use the Visual Web Developer-Beta to create a website that adds a label label to show the current execution time. and add a GridView control to it to display the table authors from the pubs database with the following code:
<%@ Page language= "VB" autoeventwireup= "false" Compilewith= "Default.aspx.vb" classname= "default_aspx"%>
! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
In the Page_Load event of the Default.aspx.vb code, add the following code
Private Sub Page_Load (ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.load
Label1.Text = CType (System.DateTime.Now (), String)
End Sub
Next, we start to configure the Web.config file. We use the following configuration file:
<?xml version= "1.0"? > >
<configuration>
<connectionStrings>
<add name= "PubsConn" connectionstring= "Initial catalog=pubs;data source= (local); user id=sa;password=123456"/>
</connectionStrings>
<system.web>
<caching>
<sqlcachedependency enabled= "true" Polltime= "500"
<databases>
<add name= "Pubs" connectionstringname= "PubsConn"/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
Among them, attention is polltime parameter, polltime parameter is millisecond, the minimum value is 500, the meaning of this parameter sets the system how long to check whether the data in the cache needs to be updated. The <database> label must indicate the database connection string that needs to be used, and must conform to the name defined in the previous <connectionStrings> (in this case, the ConnectionString name is PubsConn.)
Finally, in the header of the program, we add the page cache definition statement as follows:
The above statement defines the page output cache as 1 minutes and uses the Sqldependency,sqldependency property to define the name: Table name, which This is the name in the <databases> that we defined in Web.config, this is pubs. Authors in "Pubs:authors" is the table name Authors table to use SqlDependency. Note here that although we are using vb.net, the parameters in the SqlDependency attribute are destined to be case sensitive, such as the table name in "pubs:authors" authors must and with aspnet_ RegSql the table name authors defined under the command line.
Run our program without modifying the data in the database. The following figure:
We constantly refresh the application and find that the time is unchanged because caching is used. Now we open SQL Server, modify the name of au_id 172-32-1176, change it to red, then refresh the program, see? With the SqlDependency feature, the new modified data is reflected in the cache immediately:
This article briefly introduces the newly-added sqldependency feature in ASP.net 2.0, which has many other applications, and can refer to MSDN related information.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service