Characteristics of SqlCacheDependency in asp.net 2.0
Source: Internet
Author: User
Asp.net|cache ASP. NET in the page cache is a very useful dongdong, as long as the simple above the page with a outputcache tag, you can make the page in the development of the duration directly in the cache to automatically save the page content output, Without having to let the ASP.net engine execute the page code again, of course, saving this execution time is not the most important reason, the main reason is that if the page content is removed from the database, then you can save the connection to the database, take the data steps, this benefit can be big.
Pagecache a little bit of a problem, for example, a lot of asp.net forum home on the use of the Pagecache, so in a version posted in the latest post will not immediately reflect on the home page, but need to wait for some time. Although a bit helpless, but in some occasions (such as ebay to display the auction status of the page), this delay is not allowed.
That is to say, ASP. NET cache does not automatically according to the corresponding data in the database has changed, and the corresponding cache expiration, ASP.net 2.0 new SqlCacheDependency features make this possible. (Of course, as long as we understand the general implementation of ASP.net 2.0, we can implement this feature based on ASP.net 1.0, and then I summarize the corresponding method.) )
To apply the SqlCacheDependency feature in ASP.net 2.0, the steps are as follows (based on the Whidbey version of the PDC that you have in hand):
1, make the database support SqlCacheDependency
Under the. Net Framework 1.2 installation directory (usually windows\microsoft.net\framework\v1.2.30703), there is a aspnet_ Regsqlcache.exe, this command-line tool allows our SQL Server 7.0 or SQL Server 2000 to support the SqlCacheDependency feature,
First: "Aspnet_regsqlcache–s server name –U login id–p password –d database name –ed", this command enables the specified database to support SqlCacheDependency,
Then we add the data table to be tracked: "Aspnet_regsqlcache–s server name –U login id–p password –d database name –t The name of the data table to track –et", this command enables the specified table to support SqlCacheDependency.
What has it done behind the scenes?
First, it creates a new table in the specified database, called "AspNet_SqlCacheTablesForChangeNotification," which has three fields, "TableName" records the name of the datasheet to be tracked, " Notificationcreated "records start tracking time," Changeid "is an int type of field, and every time the data in the tracked datasheet changes, the value of the field is added by 1.
It also adds a few stored procedures to the specified database to allow the ASP.net engine to query the tracked data tables.
Then, it will give us to track the table plus a few trigger, corresponding to the INSERT, Update, delete operation, these several trigger statements is very simple, is to "aspnet_ The "Changeid" field of the record in the Sqlcachetablesforchangenotification table that corresponds to the name of the "tablename" in this traced table is added with a 1.
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. Default every 500 milliseconds to execute this stored procedure once, but can modify this interval time in web.config, my experience is this query operation is also very resource-consuming, hehe.
2, web.config configuration
The configuration inside the web.config is simpler.
<configuration>
<!--plus the appropriate database connection string-->
<connectionStrings>
<add name= "Sqlserverconnectionstring"
Connectionstring= "server=sqlserver1;uid=sa;pwd=
Password;database=portaldb "/>
</connectionStrings>
<system.web>
<!--Configure the cache section to support the SqlCacheDependency-->
As soon as you perform the insert, Update, and delete operations on this trace table, the trigger on the datasheet will "AspNet_SqlCacheTablesForChangeNotification" the Database The corresponding "Changid" field value of the appropriate record of the table is modified, and then the ASP.net engine gets the new value to know that the contents of the tracked table are changed and automatically invalidates the cache of the page.
Something 1, What about Yukon?
As you can see from the above, SQL Server does not have a built-in automatic tracking data table data changes, and then notify the ASP.net engine features (this is for sure, SQL Server has been out for a long time), so ASP.net 2.0 of the development team with a customized table, Trigger, StoredProcedure, etc. to implement data change tracking. Yukon has no need to be so troublesome, Yukon built a notification Delivery service, the services will be through the Webserver 80 port directly notify an IIS built-in listener, then the listener then notify ASP.net.
And you can note that the tracing method described above can only trace data changes at the table level, that is, the ASP.net engine can only finally learn that the data in a table has changed, and which record has changed, is not traceable, and Yukon's notification The Delivery service enables record-level tracking.
Something 2, can you implement it on the ASP.net now?
Of course, we follow the first step above (or we define a set of rules to track the changes in the table data in the database), divert to add these table, trigger or something.
Asp. NET Pagecache has a VaryByCustom attribute that enables us to define the "cache expiration" rule (specifically, it can allow us to customize the cached version of the page, but can indirectly implement a custom cache "expired", hehe), As long as we rewrite the HttpApplication.GetVaryByCustomString () method in Global.asa, this method queries the database for the "Checkdbtable=users" based on the input argument string, such as "the AspNet_SqlCacheTablesForChangeNotification "(or one of your own defined trace records), let the GetVaryByCustomString () method return the value of the" Changeid "field directly. The usage of VaryByCustom is explained in detail in the MSDN documentation.
Something 3, Golfclubshack model program
In an article in front of the blog Hall, provides a model based on the ASP.net 2.0 golfclubshack site, is very good dongdong, in that inside can see many asp.net including sqlcachedependency 2.0 of the characteristics of the embodiment. Now the online complete asp.net 2.0 model program is not much, heard that Ms has been transplanted IBuySpy to ASP.net 2.0 above, I do not know the details. The MS is still in the alpha phase of Aspnetforums 2.0 or is based on the. Net Framework 1.1 (I've always thought it was based on the latest ASP.net 2.0, installed for a long time, dizzy ... )。
Something 4, start work tomorrow!!!
Spring Festival Holiday finally finished, this spring festival did not go home, stay in Shenzhen for the new Year. The Spring Festival in Shenzhen this year is really cold, whining ... There is no broadband at home, only dial-up Internet, painful. Starting tomorrow, and start work again, do not know how many days to restore the Spring Festival reversed the biological clock.
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