Cache Technology
I. Page Cache
Full page Cache
Concept: full-page cache is the simplest cache format, and pages are common cache formats. It caches the entire page.
Syntax:
<% @ Outputcache duration = "60" varybyparam = "ID" varybycustom = "Browser" %>
Application Status
Pages that do not require frequent data updates Takes a lot of time and resources
Main attributes of the whole page Cache |
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. to specify this parameter, it must be one of the following options: Any -- default value. This means that the page output can be cached in the client browser and stored in any " downstream " clients (such as proxy servers ), or cached in Web server client -- indicates that the output cache can only be stored on the client sending the request (that is, the browser) downstream -- indicates that the output cache can be stored in any supported http1.1 cached devices (such as proxy servers) none -- indicates that the output cache is disabled for this page server (serverandclient) -- indicates that the output cache will be stored in Web server |
Varybyparam |
This attribute is a string variable separated by semicolons. The pages that pass each variable as a parameter are cached."None"Does not change with any parameter,"*"Based on all parameter changes NOTE: If noVarybycontrol, This attribute is required |
Varybycontrol |
This attribute is a string variable separated by semicolons to change the output cache of the user control. These strings represent the Server Control declared in the user control.ID NOTE: If noVarybyparam, This attribute is required |
Varybycustom |
Used to customize output cache requirements.Global. asaxIf the property value isBrowserThe cache varies with the browser name and major information version. |
Varybyheader |
Changes cache entries based on changes in the specified header. |
Example:
On the pageCodeAdd the following code to the command area
<% @ Outputcache duration = "60" varybyparam = "ID" varybycustom = "Browser" %>
Note:VarybyparamAttributeIDParameters for requests from the previous page onlyResponse. rediect ("user. aspx? Id = "+ id );
Partial page Cache( Cache replacement)
Concept: After cache replacement, the entire page is cached, and then the portion of the page that does not need to be cached is replaced.
Implement controls-- Subsitutioin Widget
1 , Add the entire page cache command
<% @ Outputcache duration = "120" varybyparam = "NONE" %>
2 , Add in the part that does not need to be cachedSubsitution Widget
<Asp: subsitution id = "subsitution1" runat = "server" methodname = "getnum"/>
3 , Write in the post codeSubsitutioin ControlMethodname Attribute Method
Protected static string getnum (httpcontext context ){
Datatable dt = getticketsdatatable (3 );Return DT. Rows [0] ["num"]. tostring ();}
Note:
Subsitutioin WidgetMethodname The method called by the property must meet three conditions.
(1) Must be static(Static) Method(2) The return type must beString (3) The parameter type must beHttpcontext Type
Ii. Data Cache( ApplicationProgramCache)
Concept: Application cache is used to store objects related to applications.Cache Class.
Naming controls
Using system. Web. caching;
Add Cache
1 , Specify the key and Value
Cache ["key"] = "value "; This method is the most convenient to add a cache. However, if you need to set the cache validity period, dependencies, and other features, it becomes powerless.
2 , UseAdd () Method-- Only used inASP. NET Add new cache items to the cache( If it is used to overwrite the existing cache items, it will fail.) .
Cache. Add ("key", "value", cachedependency, datetime, timespan, cacheitempriority, cacheitemremovedcallbac );
This method is suitable for caching that requires setting the cache validity period, dependencies, and other features.7 Parameters must be completely written to take effect, so the flexibility is not good enough.
Parameter description:
The first parameter:Key -- Indicates the key that references the cached object.( Key)
The second parameter:Value -- Indicates the object to be cached.( Value)
Third parameter:Cachedependency -- Add dependency( You can specify a file or cache key. If the file changes, the object will be deleted. If the cache key changes, the object is also deleted.)
Fourth parameter:Datetime -- Absolute expiration time( Indicates the cache data expiration time.)
Fifth parameter:Timespan -- The adjustable expiration time is also called the elastic expiration time. Specifies how long the cached data can be retained in the cache after the last access( That is to say, in the relative expiration time, the relative expiration time starts from the remaining time point of the relative expiration time.Timespan. Zero Indicates that no adjustable expiration time is specified.)()
Sixth parameter:Cacheitempriority -- Indicates the priority. When the cache is filled, the data with low priority is deleted.
Seventh parameter:Cacheitemremovedcallbac -- Callback custom Method( Example: A notification is required when a cache item is deleted from the cache.) , Its type isCacheitemremovedcallback Type
Example 1:( Specify5 Absolute expiration time in minutes)
Cache. Add ("key", "value", null, datetime. Now. addminutes (5), timespan. Zero, cacheitempriority. Normal, null );// Create a specified5 Cache with an absolute expiration time in minutes
Example 2:( Specify5 The elastic expiration time in minutes. No absolute expiration time is specified.)
Cache. Add ("key", "value", null, datetime. maxvalue, timespan. fromminutes (5), cacheitempriority. Normal, null );// Create a specified5 Cache of the flexible expiration time in minutes
Example 3:( The expiration time also depends on the modification of a file.)
// The expiration time depends onTest. xml File modification. That is, whenTest. xml When the file content is modified, the cache will expire.
Cachedependency dependency = new cachedependency ("C: \ test. xml ");
// Create a cache that expires when the dependent file is modified.
Cache. Add ("key", "value", dependency, datetime. maxvalue, timespan. fromminutes (5), cacheitempriority. Normal, null );
Example 4:( The expiration time depends on another modification in the cache.)
Cache. Add ("key1", "value", null, datetime. maxvalue, timespan. Zero, cacheitempriority. Normal, null );// When cachingKey1 When the valueKey2 Will expire
// Add love to the array
String [] dependencykeys = new string [1];
Dependencykeys [0] = "key1 ";
Cachedependency dependency = new cachedependency (null, dependencykeys );
// Create a cache that expires when it depends on another cache.
Cache. Add ("key2", "value", dependency, datetime. maxvalue, timespan. Zero, cacheitempriority. Normal, null );
Example 5:( Callback method after cache expiration)
Public void itemremovedcallback (string key, object value, cacheitemremovedreason reason ){}// Method To be called when the cache expires
// Create5 Called after the elastic expiration time in minutesItemremovedcallback Method Cache
Cache. Add ("key", "value", null, datetime. maxvalue, timespan. fromminutes (5), cacheitempriority. Normal, new cacheitemremovedcallback (this. itemremovedcallback ));
Priority Value Order |
Description |
High |
Cache items with this priority cannot be deleted when the memory is insufficient. |
Abovenormal |
Set the cache item priorityNormalOr the following cache items are more retained |
Normal |
Set the cache item priorityBelownormalAndLowCache items are retained first. |
Belownormal |
This is the second-to-last priority. The cache item with this priority is setLowCache items are retained first. |
Low |
Cache items with this priority are most likely to be deleted when the memory is insufficient. |
Default |
The default priority of the cache item isNormal |
Notremovable |
When the cache item is set to this priorityASP. NETDo not delete it from the cache even if the memory is insufficient. |
3, UseInsert ()Method--Used to overwriteASP. NETExisting cache items in the cache.This method can be used in multiple methods for overloading and is flexible to use.(But the main usage andCache. AddPoor usage)
Cache. insert ("key", "value ");
Example 1:( Add key value)
Cache. insert ("key1", "value1 ");
Example 2:( Add dependency Cache)
Cache. insert ("key", "value", system. Web. caching. cachedependecy (null, dependencies ));
Example 3:( Add valid expiration time)
Cache. insert ("key", "value", datetime. Now. addminutes (1D), system. Web. caching. cache. noslidingexpiration );
Example 4:( Add priority Cache)
Cache. insert ("key", "value", null, system. web. caching. cache. noslidingexpiration, system. web. caching. cache. noslidingexpiration, system. web. caching. cacheitempriority. high, null );
4 , Search cache
If (Cache ["key"]! = NULL)
{// Processing statement}
5 , Remove cache
(1) Auto remove
When the cache is full, expired, or the dependency changes, the cache items are automatically removed.
(2) Show remove
Cache. Remove ("key ");
Iii. Disable Cache
1 Cancel Client
<HTML> <Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "Pragma" content = "no-Cache">
</Head> <body> 2 Cancel the service Appliance
Response. Buffer = true;
Response. expiresabsolute = datetime. Now. adddays (-1 );
Response. cache. setexpires (datetime. Now. adddays (-1 ));
Response. expires = 0;
Response. cachecontrol = "no-Cache ";
Response. cache. setnostore ();
3 ,Global Inside:
Protected void application_beginrequest (Object sender, eventargs e ){
Httpcontext. Current. response. cache. setnostore ();}
<% @ Outputcache location = "NONE" %> // Withdrawal prohibited
Note:Global Apply for global variables, which are generally defined in modules and cannot be defined in program files
Public It can be defined in the module or in the program file.
4 Page Base class:
Public class pagebase: Page {
Public pagebase (){}
Protected override onload (eventargs e ){
Response. cache. setnostore ();
Base. onload ();
}}
5 , The simplest way: InURL Add some useless parameters randomly.(Ie YesURL To control the cache.)
example: http: // xxx/XXX/xxx. jsp? P = xxx