Analysis of ASP. NET data cache mechanism

Source: Internet
Author: User

What is the main mechanism of ASP. NET data cache? Let's start with our explanation:

◆ Page output cache: Save the page processing output and reuse the saved output next time.

◆ Application cache: allows you to cache generated data, such as DataSet.

(I) ASP. NET data cache page output Cache

1. Several Forms of cache output on the ASP. NET data cache page

① <%@ OutputCache Duration = "60" VaryByParam = "None" Location = "Any" %>

Location specifies where the cache is located, and Any is cached anywhere.

The results are the same within 60 seconds.

② You can also write it in the configuration file and then call the cache name of the configuration file on the page.

③ Programming:

 
 
  1. Response.Canche.SetExpires(DateTime.Now.AddSeconds(3));   
  2. Response.Canche.SetCacheabiliy(HttpCacheability.Public);   
  3. Response.Canche.SetValidUntilExpires(true); 

Equivalent:

 
 
  1. Public   =﹥   Any   
  2. Private   =﹥   Client   
  3. NoCache   =﹥   None   
  4. Server   =﹥   Server   
  5. ServerAndPrivate   =﹥ ServerAndClient  

2. ASP. NET data cache uses file dependencies to cache page output

Background: Sometimes, you may need to remove a file from the output cache when the file is changed. That is to say, the cache becomes invalid immediately after the file is changed.

 
 
  1. StringFilepath = Server. MapPath ("TextFile1.txt");
  2. Response. AddFileDependency (filepath );// Add cache Dependencies 
  3. Response. Cache. SetExpires (DateTime. Now. AddSeconds (60 ));
  4. Response. Cache. SetCacheability (HttpCacheability. Public );
  5. Response. Cache. SetValidUntiExpires (True);

3. multiple versions of ASP. NET data cache

① Cache each page version using the requested Browser

 
 
  1. ﹤%@OutputCache   Duration= "10 "   VaryByParam= "None "   VaryByCustom= "browser "%﹥  

② Cache each version of the page using parameters

 
 
  1. ﹤%@OutputCache   Duration= "60 "   VaryByParam= "City "%﹥  

This debugging can add QueryString after the url

For example,... url? City = shanghai

In the program, get this shanghai and then perform other operations. At this time, if the parameter is still passed to shanghai, it will not be in the program.

4. ASP. NET data cache dynamically updates the part of the cache page. There are three methods to achieve partial non-Cache

① Substitution control is declared

 
 
  1. <Asp: Substitution ID ="Substitution1"Runat ="Server"MethodName ="GetCurrentDateTime"/>
  2. PublicStaticString GetCurrentDateTime (HttpContext context)
  3. {
  4. ReturnDateTime. Now. ToString ();
  5. }
  6. // The method signature must be consistent with the delegate Signature 

② Use the Substitution control API programmatically

Response. WriteSubstitution (new HttpResponseSubstitutionCallback (GetCurrentDateTime ))

③ Use the AdRotator control implicitly

This control is never cached.

(Ii) ASP. NET data cache the cache on which SQL Server depends, which is very useful

When the table data changes, the cache is cleared.

1. Enable cache notification for SQL Server in ASP. NET data cache

 
 
  1. aspnet_regsql.exe   -S   ﹤Server﹥   -U   ﹤Username﹥   -P   ﹤Password﹥   
  2. -ed   -d   Northwind   -et   -t   Employees  

Server: Server

Username: User Name

Password: Password

Northwind: Database

Employees: Table

2. Configure web pages for the cache function in ASP. NET data cache

 
 
  1. ﹤%@OutputCache   Duration= "3600 "   SqlDependency= "Northind:Employees "   VaryByParam= "none "%﹥  

3. Set the cache configuration for ASP. NET data caching in the Web. config file.

 
 
  1. ﹤caching﹥   
  2.   ﹤sqlCacheDependency   enabled= "true "   pollTime= "1000 "﹥   
  3.     ﹤database﹥   
  4.       ﹤add   name= "Northind "   connectionStringName= "... "   pollTime   =   "1000 "   /﹥   
  5.       ﹤/database﹥   
  6.   ﹤/sqlCacheDependency﹥   
  7. ﹤/caching﹥  

The content of ASP. NET data cache will be introduced here, hoping to help you understand ASP. NET data cache.

  1. Analysis of ASP. NET programming standards and Their encoding specifications
  2. Test Specification Analysis of ASP. NET Programming specifications
  3. Introduction to the five Data Controls of ASP. NET
  4. Comparison of ASP. NET data binding controls
  5. Comparison between ASP. NET's GridView and DataGrid controls

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.