Sqlcachedependency in ASP. NET 2.0

Source: Internet
Author: User
ASP. NET page cache is very useful, as long as you simply add an outputcache tag on the top of the page, this allows the page to directly output the page content automatically stored in the cache in the specified duration, without the need for ASP. net engine re-execution page Code Of course, saving execution time is not the main reason. The main reason is that if the page content is extracted from the database, you can skip the steps of connecting to the database and getting data, this benefit is even greater.

Pagecache has some minor problems, such as many ASP. the homepage of the Net Forum uses pagecache, so the latest post posted in a single version will not be immediately reflected on the homepage, but will need to wait for a while. Although a bit helpless, in some occasions (for example, eBay is used to display the page of the item auction status), this delay is not allowed.

That is to say, Asp. the cache in. Net does not automatically change according to the corresponding data in the database, and the corresponding cache expires. the sqlcachedependency feature added in net 2.0 makes this possible. (Of course, as long as we understand the general implementation method in ASP. NET 2.0, we can implement this Feature Based on ASP. NET 1.0. I will summarize the corresponding methods later .)

To apply the sqlcachedependency feature in ASP. NET 2.0, follow these steps (based on the Whidbey PDC version in your hands ):

1. Make the database support sqlcachedependency
In the. NET Framework 1.2 installation directory (usually Windows \ Microsoft. NET \ framework \ v1.2.307038.0, there is an aspnet_regsqlcache.exe. This command line tool enables our sqlserver 7.0 or sqlserver 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 name of the data table to be tracked-ET ", this command enables the specified table to support sqlcachedependency.

What did it do behind the scenes?

First, it creates a table named "aspnet_sqlcachetablesforchangenotification" in the specified database. The table has three fields, and "tablename" records the name of the data table to be tracked, icationicationcreated records the start time of tracing. changeid is an int type field. When the data in the tracked data table changes, the value of this field is added with 1.

It also adds several stored procedures to the specified database to enable the ASP. NET engine to query and track data tables.

Then, it will add several triggers to the table to be tracked, corresponding to insert, update, and delete operations respectively. These trigger statements are very simple, in the "aspnet_sqlcachetablesforchangenotification" table, the corresponding "tablename" field adds 1 to the "changeid" Field of the record of the name of the tracked table.

ASP. the net engine executes the stored procedure "aspnet_sqlcachepollingstoredprocedure" that directly returns the content of the "aspnet_sqlcachetablesforchangenotification" table to ASP. the net engine knows which table the data changes. The stored procedure is executed every 500 milliseconds by default. However, you can modify the interval in Web. config. In my experience, this query operation is resource-consuming.

2. Web. config Configuration

The configuration in Web. config is much simpler.


<Configuration>

<! -- Add the appropriate database connection string -->

<Connectionstrings>

<Add name = "sqlserverconnectionstring"

Connectionstring = "Server = sqlserver1; uid = sa; Pwd =

Password; database = portaldb "/>

</Connectionstrings>

<System. Web>

<! -- Configure a cache segment to support sqlcachedependency -->

<Cache>

<Sqlcachedependency enabled = "true" polltime = "500">

<Databases>

<Add name = "portaldb"

Connectionstringname = "sqlserverconnectionstring"/>

</Databases>

</Sqlcachedependency>

</Cache>

</System. Web>

</Configuration>



3. Specify the sqlcachedependency attribute in the outputcache tag on the page:



<% @ Outputcache duration = "9999" varybyparam = "NONE" sqldependency = "portaldb: name of the tracked data table" %>



As long as the insert, update, and delete operations are performed on the tracked table, the trigger on the data table modifies the value of the corresponding changid field in the "aspnet_sqlcachetablesforchangenotification" table of the database, and then ASP. the net engine will get a new value to know that the content of the tracked table has changed, and the cache of this page will automatically become invalid.



What about Yukon?



As you can see above, sqlserver does not have built-in automatic tracking of data changes in the data table, and then notifies ASP. net engine function (this is affirmative, sqlserver has been out for N long), so ASP.. NET 2.0 developers add customized tables, triggers, storedprocedure, and so on to track data changes. Yukon does not need to be so troublesome. Yukon has a built-in notification delivery service. This service will directly notify an IIS built-in listener through port 80 of Webserver, and then this listener will notify ASP.. net.



You can also note that the tracing method described above can only track table-level data changes, that is, Asp. the net engine can only know that the data of a table has been changed, but which record has been changed cannot be tracked, the notification delivery service of Yukon can trace records at the record level.



2. Can I implement it on the current ASP. NET?



Of course, we can follow the first step described above (or define a set of rules to implement tracking of table data changes in the database ), add the tables, triggers, and so on.



ASP. net pagecache has a varybycustom attribute, which allows us to define the "cache expiration" rule (to be exact, in fact, it allows us to customize the version of the cache page, but indirectly it can achieve custom cache "Expiration", haha), as long as we are in the global. asa rewrite httpapplication. getvarybycustomstring () method. This method queries the "aspnet_sqlcachetablesforchangenotification" in the database based on the input parameter string, for example, "checkdbtable = users" (or a custom tracking record table ), directly let the getvarybycustomstring () method return the value of the "changeid" field. The usage of varybycustom is described in detail in the msdn document.



3. golfclubshack demonstration Program

An article in front of a blog Article Which provides an ASP-based. NET 2.0 golfclubshack site demonstration program, is a very good stuff, in which you can see a lot of ASP including sqlcachedependency. NET 2.0 features. Currently, there are not many complete examples of ASP. NET 2.0 on the Internet. I have heard that Ms has ported ibuyspy to ASP. NET 2.0. I don't know how to use it. While the aspnetforums 2.0 in the Alpha stage is still based on. net Framework 1.1 (I used to think it is based on the latest ASP. NET 2.0, installed for a long time, dizzy ...).



4. start work tomorrow !!!

After the Spring Festival holiday is over, I stayed in Shenzhen for the Chinese New Year. This spring festival in Shenzhen is really cool... There is no broadband at home, and I can only dial up the Internet, which is painful. Starting from tomorrow, I have to start working again. I don't know how long it will take for me to recover the clock that has been reversed during the Spring Festival.
Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.