Response.CacheControl = "No-cache"
<%@ OutputCache duration= "1" varybyparam= "None"%>
Cache
Caching mechanism
Page output Caching: Save the page processing output, next reuse the saved output
Application caching: Allows caching of generated data, such as a dataset
㈠ page Output Caching
1, page output cache in several forms
①<%@ OutputCache duration= "All" varybyparam= "None" location= "any"%>
Location specifies where the cache is, and any place is cached.
What you see within 60 seconds is the same.
② can also be written in the configuration file, and then the cache name of the configuration file is invoked on the page.
③ in a programmatic way:
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. Use file dependencies to cache page output
Background: Sometimes you might want to remove an item from the output cache when a file changes. This means that the cache is invalidated immediately after the file has been 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. Cache Multiple Versions
① to cache versions of a page using the requested browser
<% @OutputCache duration= "Ten" varybyparam= "None" varybycustom= "Browser"%>
② use parameters to cache versions of a page
<% @OutputCache duration= "varybyparam=" "City"%>
This debug can be added to the URL after querystring
such as: ... URL? City=shanghai
In the program to get this Shanghai and then do other operations, this time if the parameters passed or Shanghai it will not be in the program.
4, Dynamic update cache page part, there are three ways to implement partial not cache
① used substitution control declaratively
<asp:substitution id= "Substitution1" runat= "Server" methodname= "GetCurrentDateTime"/>
public static string GetCurrentDateTime (HttpContext context)
{
return DateTime.Now.ToString ();
}
Method signature must be consistent with delegate signature
② uses the substitution control API in a programmatic way
Response.WriteSubstitution (New HttpResponseSubstitutionCallback (GetCurrentDateTime))
③ to use AdRotator controls in an implicit manner
This control will never be cached.
㈡sql server-dependent caching, very useful
Clears the cache when table data has changed
1. Enable cache notification for SQL Server
Aspnet_regsql.exe-s <Server>-u <Username>-P <Password>
-ed-d northwind-et-t Employees
Server: Servers
Username: User Name
Password: Password
Northwind: Database
Employees: Table
2. Configuring Web pages for caching functions
<% @OutputCache duration= "3600" sqldependency= "Northind:employees" varybyparam= "None"%>
3. Set cache configuration in Web.config file
<caching>
<sqlcachedependency enabled= "true" polltime= "1000" >
<database>
<add name= "Northind" connectionstringname= "..." polltime = "1000"/>
</database>
</sqlCacheDependency>
</caching>
Name here, if the database name
Asp. NET, there are many kinds of data caching, in general, large data use cache object caching, user identity information is cached with session object, and small data is cached with cookie object in client.
where cache and session should belong to the system-level cache, cookies belong to the user client's cache
In addition to the caching of user controls, I reprint an article detailing the caching of user controls
<%@ OutputCache duration= "*" varybyparam= "*"%>
As with other page directives, the directive should appear at the top of the ASPX page, before any output. It supports five properties (or parameters), of which two are required.
Duration Required Properties. The time, in seconds, that the page should be cached. Must be a positive integer.
Location specifies where the output should be cached. If you want to specify this parameter, you must have the following options: Any, Client, downstream, None, server, or ServerAndClient.
VaryByParam Required Properties. The name of the variable in request, which should produce a separate cache entry. "None" indicates no change. The "*" can be used to create a new cache entry for each variant array. Use ";" between variables. are separated.
VaryByHeader changes the cached entry based on the changes in the specified header.
VaryByCustom allows custom changes to be specified in Global.asax (for example, "Browser").
Use a combination of the required duration and VaryByParam options to handle most situations. For example, if your product catalog allows users to view catalog pages based on CategoryID and page variables, you can cache the product catalog for a period of time with the VaryByParam parameter value of "Categoryid;page" (if the product is not always changing, an hour is acceptable, Therefore, the duration is 3,600 seconds). This creates a separate cache entry for each catalog page of each kind. Each entry will be maintained for one hours from its first request.
VaryByHeader and VaryByCustom are primarily used to customize the appearance or content of a page based on the client accessing the page. The same URL may need to render output for both the browser and the mobile client, so different content versions are cached for different clients. Alternatively, the page might have been optimized for IE, which should be canceled for Netscape or opera. The latter example is very common, and we will provide an example that shows how to achieve this goal:
Example: VaryByCustom is used to support browser customization
In order for each browser to have a separate cache entry, the VaryByCustom value can be set to "browser". This feature is already built into the cache module, and a separate page cache version is inserted for each browser name and major version. <%@ OutputCache duration= varybyparam= "None" varybycustom= "Browser"%>
Example:
<%@ OutputCache duration= "*" varybyparam= "*"%>
The example caches the user control for 60 seconds and creates a separate cache entry for each change to the query string, for each page that contains the control.
<%@ OutputCache duration= varybyparam= "None"
Varybycontrol= "Categorydropdownlist"%>
The example caches the user control for 60 seconds and will target the Categorydrop
Downlist each different value of the control, creating a separate cache entry for each page on which the control resides.
<%@ OutputCache duration= varybyparam= "None" varybycustom= "Browser"
Shared= "true"%>
Finally, the example caches the user control for 60 seconds and creates a cache entry for each browser name and major version. The cached entries for each browser are then shared by all pages that reference the user control (as long as all pages refer to the control with the same ID).
The
The following example shows how to set the duration of output caching for a page or user control.
<%@ OutputCache duration= varybyparam= "None"%>