Cache overview on msdn:Http://msdn2.microsoft.com/zh-cn/library/726btaeh%28VS.80%29.aspx I. Page output Cache 1. Set ASP. NET page cache in two ways 1.1 set ASP. NET page cache in declarative Mode The method for setting the ASP. NET page cache in declarative mode is to use the @ outputcache command in the page. Its common attributes are as follows: Program code <% @ outputcache duration = "" varybyparam = "" varybycontrol = "" varybyheader = "" varybycustom = "" cacheprofile = "" location = "%>Duration: Set the cache expiration time in seconds. Varybyparam: used to make the cache output vary with query strings. Multiple query characters are separated by semicolons. Varybycontrol: Used to make cache output vary with control values. Varybyheader: used to make the cache output vary with the HTTP header of the request. Varybycustom: used to make the cache output vary with the browser type or custom string. Cacheprofile: used in combination with the configuration file. Location: sets the page's cacheability. The values include any, client, downstream, none, server, and serverandclient. Note: when using the @ outputcache command, a varybyparam attribute must be included; otherwise, a analyzer error will occur. If you do not want to use the functions provided by the varybyparam attribute, set the value to "NONE ". @ Outputcache command example ① Cache each version of the page using parameters: Program code <% @ outputcache duration = "60" varybyparam = "city" %> Note: to change the output cache based on multiple parameters, include a list of parameter names separated by semicolons (;). If you want to change the cache based on all parameter values, set the varybyparam attribute to asterisk (*). If you do not want to change the cache value based on the parameter value, set the varybyparam attribute to "NONE ". ② Use the HTTP header to cache versions of a page: Program code <% @ outputcache duration = "60" varybyparam = "NONE" varybyheader = "Accept-language" %>Note: to change the cached content based on multiple headers, use semicolon (;) as the separator to include the list of header names. If you want to change the cached content based on all header values, set the varybyheader attribute to an asterisk (*). ③ Use the requested browser to cache different pages: Program code <% @ outputcache duration = "10" varybyparam = "NONE" varybycustom = "Browser" %> ④ Use a custom string to cache each version of the page: Program code <% @ outputcache duration = "10" varybyparam = "NONE" varybycustom = "minorversion" %> Note: it must also be appliedProgramIn the global. asax file of, override the getvarybycustomstring method to specify the output cache behavior of the custom string. See: http://msdn2.microsoft.com/zh-cn/library/5ecf4420 (vs.80). aspx ⑤ Combined with the configuration file: Add the following XML as a sub-item of the system. Web element: Program code <! -- Caching section group --> <Caching> <Outputcachesettings> <Outputcacheprofiles> <Add name = "appcache1" enabled = "true" Duration = "60"/> </Outputcacheprofiles> </Outputcachesettings> </Caching> @ Outputcache command: Program code <% @ outputcache cacheprofile = "appcache1" varybyparam = "NONE" %> In this way, we can change the cache behavior from a single configuration file, without having to edit the @ outputcache command on each page, and set different cache rules as needed, and then apply it to the separate pages of each group. 1.2 set ASP. NET page cache programmatically The method for setting the ASP. NET page cache programmatically is on the pageCode, Call the cache attribute of the response object: Program code response. cache. setexpires (datetime. Now. addseconds (60); // set the cache expiration time Response. cache. setcacheability (httpcacheability. Public); // you can specify the page cache. Response. cache. setvaliduntilexpires (true); // cache ignore cache-control Invalid Header Example ① Cache each version of the page using parameters: Program Code protected void page_load (Object sender, eventargs E) { Response. cache. setexpires (datetime. Now. addminutes (1.0 )); Response. cache. setcacheability (httpcacheability. Public ); Response. cache. setvaliduntilexpires (true ); Response. cache. varybyparams ["Zip"] = true; } Note: to change the cached content based on multiple parameters, set the varybyparams attribute multiple times. ② Use the HTTP header to cache versions of a page: Program Code protected void page_load (Object sender, eventargs E) { Response. cache. setexpires (datetime. Now. addminutes (1.0 )); Response. cache. setcacheability (httpcacheability. Public ); Response. cache. setvaliduntilexpires (true ); Response. cache. varybyheaders ["Accept-language"] = true; } Note: to change the cached content based on multiple headers, you must set multiple values in the varybyheaders attribute. To change the cached content based on all headers, set varybyheaders ["varybyunspecifiedparameters"] to true. ③ Use the requested browser to cache different pages: Program Code protected void page_load (Object sender, eventargs E) { Response. cache. setexpires (datetime. Now. addminutes (1.0 )); Response. cache. setcacheability (httpcacheability. Public ); Response. cache. setvaliduntilexpires (true ); Response. cache. setvarybycustom ("Browser "); } ④ Use a custom string to cache each version of the page: Program Code protected void page_load (Object sender, eventargs E) { Response. cache. setexpires (datetime. Now. addminutes (1.0 )); Response. cache. setcacheability (httpcacheability. Public ); Response. cache. setvaliduntilexpires (true ); Response. cache. setvarybycustom ("minorversion "); }Note: In the global. asax file of the application, rewrite the getvarybycustomstring method to specify the output cache behavior of the custom string. 2. Several Models of page output Cache 2.1 full page cache:When the location of ASP. NET page cache is set to occur on the page, it is the whole page cache. 2.2 page cache (control cache ):When you set the ASP. NET page cache location on the user control, that is, the control cache. 2.3 paging caches (replaced after cache ):The substitution control is declared on the entire cache page, the substitution control API is used programmatically, or the adrotator control is implicitly used, that is, the cache is replaced. Cache replacement example (using the substitution control in Declaration) Aspx code: Program code <asp: Label id = "label1" runat = "server" text = "label" width = "276px"> </ASP: Label> <Br/> <Asp: substitution id = "substitution1" runat = "server" methodname = "nocache"/> Aspx. CS code: Program Code protected void page_load (Object sender, eventargs E) { Label1.text = datetime. Now. tostring (); } Protected static string nocache (httpcontext context) { Return datetime. Now. tostring (); } Note: The methodname attribute value of the substitution control is the name of a method (nocache in this example). The requirement for this method is that the parameter type accepted by the substitution control must be httpcontext and the return value type is string, it must also be a static method! 2. Application Cache 1. Create Method 1: cache ["cachename"] = "cachevalue "; Method 2: cache. insert (string key, object value, system. web. caching. cachedependency dependencies, datetime absoluteexpiration, timespan slidingexpiration, system. web. caching. cacheitempriority priority, system. web. caching. cacheitemremovedcallback onremovecallback ); Method 3: cache. add (string key, object value, system. web. caching. cachedependency dependencies, datetime absoluteexpiration, timespan slidingexpiration, system. web. caching. cacheitempriority priority, system. web. caching. cacheitemremovedcallback onremovecallback ); The difference between the add method and the insert method is that the add method returns the object you added to the cache. In addition, if the add method is used and an item with the same name as an existing item already exists in the cache, this method will not replace this item and will not cause an exception. Example ① Add items to the cache by using the insert method: Program code cache. insert ("cacheitem2", "cached Item 2 "); ② Add items to the cache by specifying dependencies: Program code string [] dependencies = {"cacheitem2 "}; Cache. insert ("cacheitem3", "cached Item 3", new system. Web. caching. cachedependency (null, dependencies )); ③ Add the items with an expiration Policy to the cache: Program code cache. insert ("cacheitem6", "cached Item 6", null, datetime. Now. addminutes (1D), system. Web. caching. cache. noslidingexpiration ); ④ Add items with priority settings to the cache: Program code cache. insert ("cacheitem8", "cached Item 8 ", Null, system. Web. caching. cache. noabsoluteexpiration, System. Web. caching. cache. noslidingexpiration, System. Web. caching. cacheitempriority. High, null ); 2. Search Program code string cachedstring; Cachedstring = (string) cache ["cacheitem"]; If (cachedstring = NULL) { Cachedstring = "www. mzwu. com "; Cache. insert ("cacheitem", cachedstring ); } Note: The information stored in the cache is easy to lose, that is, the information may be removed from ASP. NET. Therefore, we recommend that you first determine whether the information is cached. If not, add it to the cache again and then retrieve the item. 3. Remove Program code cache. Remove ("mydata1 "); |