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:
- Response.Canche.SetExpires(DateTime.Now.AddSeconds(3));
- Response.Canche.SetCacheabiliy(HttpCacheability.Public);
- Response.Canche.SetValidUntilExpires(true);
Equivalent:
- Public =﹥ Any
- Private =﹥ Client
- NoCache =﹥ None
- Server =﹥ Server
- 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.
- StringFilepath = Server. MapPath ("TextFile1.txt");
- Response. AddFileDependency (filepath );// Add cache Dependencies
- Response. Cache. SetExpires (DateTime. Now. AddSeconds (60 ));
- Response. Cache. SetCacheability (HttpCacheability. Public );
- Response. Cache. SetValidUntiExpires (True);
3. multiple versions of ASP. NET data cache
① Cache each page version using the requested Browser
- ﹤%@OutputCache Duration= "10 " VaryByParam= "None " VaryByCustom= "browser "%﹥
② Cache each version of the page using parameters
- ﹤%@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
- <Asp: Substitution ID ="Substitution1"Runat ="Server"MethodName ="GetCurrentDateTime"/>
- PublicStaticString GetCurrentDateTime (HttpContext context)
- {
- ReturnDateTime. Now. ToString ();
- }
- // 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
- aspnet_regsql.exe -S ﹤Server﹥ -U ﹤Username﹥ -P ﹤Password﹥
- -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
- ﹤%@OutputCache Duration= "3600 " SqlDependency= "Northind:Employees " VaryByParam= "none "%﹥
3. Set the cache configuration for ASP. NET data caching in the Web. config file.
- ﹤caching﹥
- ﹤sqlCacheDependency enabled= "true " pollTime= "1000 "﹥
- ﹤database﹥
- ﹤add name= "Northind " connectionStringName= "... " pollTime = "1000 " /﹥
- ﹤/database﹥
- ﹤/sqlCacheDependency﹥
- ﹤/caching﹥
The content of ASP. NET data cache will be introduced here, hoping to help you understand ASP. NET data cache.
- Analysis of ASP. NET programming standards and Their encoding specifications
- Test Specification Analysis of ASP. NET Programming specifications
- Introduction to the five Data Controls of ASP. NET
- Comparison of ASP. NET data binding controls
- Comparison between ASP. NET's GridView and DataGrid controls