Basic concepts of ASP. NET Cache Mechanism

Source: Internet
Author: User

ASP. NET cache mechanism Glossary

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.

ASP. NET cache mechanism concept (I): page output Cache

1. Several Forms of page output Cache

① <% @ 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. cache page output using file Dependencies

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.

String filepath = 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 cached versions

① 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. There are three ways to dynamically update the part of the cache page to achieve partial non-Cache

① Substitution control is declared

<Asp: Substitution ID = "Substitution1" runat = "server" MethodName = "GetCurrentDateTime"/>

Public static string GetCurrentDateTime (HttpContext context)

{

Return DateTime. 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.

ASP. NET cache mechanism concept (II): SQL Server dependent cache, very useful

When the table data changes, the cache is cleared.

1. Enable cache notification for SQL Server

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 the webpage for the cache Function

<% @ OutputCache Duration = "3600" SqlDependency = "Northind: Employees" VaryByParam = "none" %>

3. Set cache configuration in the Web. config file

<Caching>

<SqlCacheDependency enabled = "true" pollTime = "1000">

<Database>

<Add name = "Northind" connectionStringName = "..." pollTime = "1000"/>

</Database>

</SqlCacheDependency>

</Caching>

// The name here is the database name

There are many data caching methods in ASP. NET. Generally, large data is cached Using Cache objects, user identity information is cached using Session objects, and small data is cached on the client using Cookie objects.

The Cache and Session should belong to the system-level Cache, And the Cookie belongs to the Cache of the user client.

In addition, there is also the cache of user controls. I will repost an article to introduce in detail the cache of user controls.

<% @ OutputCache Duration = "60" VaryByParam = "*" %>

Like other page commands, this command should appear at the top of the ASPX page, that is, before any output. It supports five attributes or parameters), two of which are required.

Duration Required attribute. The time when the page should be cached, in seconds. Must be a positive integer.

Location specifies the Location where the output should be cached. If you want to specify this parameter, it must be Any, Client, Downstream, None, Server, or ServerAndClient.

Required attribute of VaryByParam. The name of the variable in the Request. These variable names should generate separate cache entries. "None" indicates no change. "*" Can be used to create a cache entry for each variable group. Variables are separated.

VaryByHeader changes cache entries based on changes in the specified header.

VaryByCustom allows you to specify custom changes in global. asax, for example, "Browser ").

The combination of the required Duration and VaryByParam options can be used to handle most cases. For example, if your product directory allows users to view the directory Page Based on categoryID and page variables, you can set the parameter value to "categoryID; page "VaryByParam caches the product directory for a period of time. If the product is not changed at any time, it is acceptable for one hour. Therefore, the duration is 3600 seconds ). This creates separate cache entries for each directory page of each category. Each entry is counted from its first request for one hour.

VaryByHeader and VaryByCustom are mainly used to customize the appearance or content of the page based on the client accessing the page. The same URL may need to be rendered and output for both the browser and mobile client. Therefore, different content versions must be cached for different clients. Alternatively, the page may have been optimized for IE. For Netscape or Opera, the optimization function should be canceled. The next example is very common. We will provide an example to illustrate how to achieve this goal:

Example: VaryByCustom is used to support browser customization.

To make each browser have a separate cache entry, the value of VaryByCustom can be set to "browser ". This function has been built into the cache module and will insert a separate page Cache version for each browser name and major version. <% @ OutputCache Duration = "60" VaryByParam = "None" VaryByCustom = "browser" %>

Example:

<% @ OutputCache Duration = "60" VaryByParam = "*" %>

In this example, the user control is cached for 60 seconds, and a separate cache entry is created for each change of the query string and for each page where the control is located.

<% @ OutputCache Duration = "60" VaryByParam = "none"

VaryByControl = "CategoryDropDownList" %>

This example caches the user control for 60 seconds and

Each different value of the DownList control creates a separate cache entry for each page where the control is located.

<% @ OutputCache Duration = "60" VaryByParam = "none" VaryByCustom = "browser"

Shared = "true" %>

Finally, this example caches the user control for 60 seconds and creates a cache entry for each browser name and major version. Then, the cache entries of Each browser will be shared by all pages that reference this user control, as long as all pages reference this control with the same ID ).

The following example shows how to set the duration of page or user control output cache.

<% @ OutputCache Duration = "100" VaryByParam = "none" %>

The above introduces some basic concepts of ASP. NET cache mechanism.

  1. ASP. NET cache mechanism: balance between development efficiency and Optimization
  2. . NET distributed cache for Memcached execution speed detection
  3. How to Avoid ASP. NET cache occupying system resources
  4. . NET Cache Mechanism discussion and Comparison
  5. Overview ASP. NET Cache Mechanism

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.