"Go" sqlcachedependency use powerful features

Source: Internet
Author: User

The original Published time: 2009-10-25--from my Baidu article [imported by moving tools]

Recently I was busy studying load balancing, concurrency tolerance and other performance optimization problems, ASP. NET has too many powerful features to wait for learning and digging. Today, Bosnma is working with you to explore how the caching mechanism in ASP. Can help us reduce server load on high-traffic websites. What is a cacheCaches are ubiquitous, especially for high-traffic sites. Some pages, especially pages with one or more database operations, can have too much load on the database server, which is often unnecessary. For example, a company's product List page may be one hours, a day or even longer time has not changed, unfortunately, every user browsing the page will cause database connection and query. The cache is designed to solve this problem. a simple implementation of cachingThe simplest way to implement caching in ASP. is to add a OutputCache tag to the page. <% @ OutputCache Duration = "VaryByParam" = "*"%> where Duration specifies the expiration time of the cache. The above sentence indicates that the database query operation of the page will be automatically saved to the cache in 20 seconds, no longer duplicate the database, and greatly reduce the load of the database in the high traffic website. No free lunchPagecache simple and easy to use, but a little bit of a problem. For example, in a forum on the first page will often use Pagecache, so that the latest posts will not immediately appear on the first page, but need to wait a period of time (the cache expires) before the appearance, and this delay in some cases is completely disallowed. For example, a shopping mall site, someone has ordered the last batch of goods, and because of the caching mechanism, other people to view the status is still pre-order (not ordered), I think you do not want two people to buy your only one item. Asp. NET cache is not automatically based on the data in the database changed, and the corresponding cache expires, the new SqlCacheDependency feature in ASP. 2.0 makes this possible. Our theme for today is drawn from this. the powerful SqlCacheDependencySqlCacheDependency reduces the database load by caching in the context of guaranteed update synchronization. So how do you use it? 1, make the database support SqlCacheDependency
In the installation directory of the. Net Framework 2.0 (usually windows\microsoft.net\framework\v2.0.xxxxx), there is a aspnet_ Regsql.exe, this command-line tool helps us to enable the database to support the SqlCacheDependency feature,

First: "Aspnet_regsql–s server name –U login name id–p password –d database name –ed", this command enables the specified database to support SqlCacheDependency, and then we add the data table to be traced: "Aspnet_regsql–s service Name –U login id–p password –d database name –t data table –et ", this command causes the specified table to support SqlCacheDependency. After the instruction is run, a new table is created in the specified database called "AspNet_SqlCacheTablesForChangeNotification", which has three fields, "TableName" records the name of the data table to be traced, " Notificationcreated "The time at which the record begins to be traced," Changeid "is a field of type int, and the value of this field is incremented by 1 whenever the data of the tracked data table changes. It also adds several stored procedures to the specified database to allow the ASP to query the tracked data tables. It then adds a few trigger to the table we want to track, corresponding to the INSERT, Update, delete operations, which are simple trigger statements that "aspnet_ The "Changeid" field in the Sqlcachetablesforchangenotification "table corresponding to" TableName "is the name of the tracked table plus a 1. Asp. NET engine by executing it plus the stored procedure "Aspnet_sqlcachepollingstoredprocedure", this stored procedure directly returns "AspNet_SqlCacheTablesForChangeNotification The contents of the table allow the ASP. NET engine to know which table's data is changed. The default is to execute this stored procedure every 500 milliseconds, but this interval can be modified in Web. config.

2. Web. config configuration
< system.web > <!--Configure the cache node so that the site supports SqlCacheDependency--< cache > < Sqlcachedepen Dency enabled = "true" Polltime = "$" > < databases > < add name = "Cachedb" Connect Ionstringname = "CS"/> </databases > </sqlcachedependency > </cache > </S Ystem.web > 3. Specify the SqlCacheDependency feature in the OutputCache tab on the page:<% @ OutputCache Duration = "9999" VaryByParam = "None" SqlDependency = "Cachedb:tablename"%> where TableName is a tracked table name, As soon as you perform an INSERT, Update, delete operation on this tracked table, the trigger on the datasheet will "AspNet_SqlCacheTablesForChangeNotification" in the database The corresponding "Changid" field value of the corresponding record of the table is modified, and then the ASP. NET engine will get a new value to know that the contents of the tracked table have changed, automatically invalidate the cache of this page finallyThe same site, the same features, security, concurrency, fault tolerance is very different, and the site is not good or bad to see how much he has a fancy function or how gorgeous interface, but whether he is practical, stable, efficient, safe. An extensive and profound, it seems that many things, are "more learning more shallow."

"Go" sqlcachedependency use powerful features

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.