Asp.net supports three types of cache.
I want to write a quick technical overview, but it may be too much. The purpose of the technical overview is to describe the technical points in the fastest and simplest way, which is also the most efficient way of disseminating knowledge I hope.
1. Page/control cache
2. Application level cache
3. Browser client cache
From the perspective of implementation, the page/control cache and application-level cache are stored in the server memory. As the memory becomes tight, these contents may be deleted in advance before they become invalid. (The characteristics of cache determine that these contents can be deleted with confidence). The cache of the browser client is stored in the cache of the client browser. For example, the temporary folder of IE functions as a cache. Every time a user requests a page, the browser will first search the cache to see if there is cache content that meets the requirements and has not expired. If so, it will read it directly from the cache and skip network transmission.
The following demonstrates the specific writing method in Asp.net:
1. The page/control cache can be declared in the aspx, ascx file, or it can be declared in the code behind.
<%@ OutputCache Duration="#ofseconds"
Location="Any | Client | Downstream | Server | None |
ServerAndClient "
Shared="True | False"
VaryByControl="controlname"
VaryByCustom="browser | customstring"
VaryByHeader="headers"
VaryByParam ="parametername"
CacheProfile="cache profile name | ''"
NoStore="true | false"
SqlDependency="database/table name pair | CommandNotification"
%>
The main parameters are:
Duration: cache effective time, unit seconds
Shared: It is only valid for controls. When a control can be applied to multiple pages at the same time, can multiple pages share this cache without each page maintaining its own cache for this control?
VaryByControl: The cache changes with the ID of the control.
VaryByCustom: The cache changes with a user-defined variable. The variable is specified here and should be implemented in Global.ascx:
public override string GetVaryByCustomString (
HttpContext context,
string custom
)
this statement The custom parameter is the variable declared in OutPutcache. Different cache contents are represented by returning different string values for different custom parameters in this function.
VaryByHeader, VaryByParam, and CacheProfile also set different cache versions.
NoStore means that the contents of the cache are not allowed to be written to storage devices other than memory. This is a requirement for content with relatively high security.
SqlDependency is related to the database.
2. Application-level cache
can only be obtained in the code behind, and its characteristic is that it can implement any cache logic it needs.
Can be obtained through Page.Cache, the class name is System.Web.Caching.Cache
3. The browser client cache
can only be obtained in codebehind and can be specified through Response.Cache, which is an HttpCachePolicy object.
In addition: You can specify a TimeSpan as the cache time, without converting it to seconds each time.
public TimeSpan(long ticks);
public TimeSpan(int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds , int milliseconds);
If you want the cache to retain one hour, one minute, and one second, you can directly use
new TimeSpan(1,1,1)
instead of first calculating
1 hour, 1 minute, and 1 second = 3600+60+1=3661 seconds, and then transfer Enter 3661 as the validity period.
http://www.cnblogs.com/ThomasNet/archive/2006/11/26/573104.html