The cache mechanism provided by ASP. NET plays a vital role in improving the page performance. On the other hand, the use of the cache will also cause information update delay. It is sometimes difficult to quickly update cache data. Program Staff problems. Based on my experience, I have summarized the following methods and summarized several common situations. If you have better methods, please add them.
(1) cache object Cache Cache object provision Code High-level caching, powerful functions, and high operability. The method for updating the cache is simple. You only need to call the cache. Remove (key) method to clear the specified cache. The Code is as follows:
Httpruntime. cache. Remove (cacheenmu. Key. tostring ());
Run the following code to clear all cache caches:
Idictionaryenumerator cacheenmu = httpruntime. cache. getenumerator (); While (cacheenmu. movenext () {httpruntime. cache. Remove (cacheenmu. Key. tostring ());}
(2) page-level cache outputcache Compared with cache objects, page-level outputcache is much easier to use, and page Code does not need to be changed, you only need to add the <% @ outputcache %> Statement on the top of the page to cache all content on the current page. Of course, there are some advantages and disadvantages. outputcache only provides a limited number of parameters and is not as flexible as cache objects when cache needs to be cleared. However, there is still a way. NET provides the removeoutputcacheitem () method to clear the page-level output cache. The usage is as follows:
Httpresponse. removeoutputcacheitem (fname); // fname is the absolute path of the cache page to be cleared, such as/article/read. aspx.
(3) User Control-level cache outputcache The user control-level cache is similar to the page-level cache. The difference is that the user control can only cache partial pages. This is a good solution for pages that do not require full-page cache. However, the removeoutputcacheitem () method of clearing the page cache is invalid for the user control. After reading a lot of information, the conclusion is that. Net does not provide a method to directly clear the user control output cache. My method is to use outputcache dependency item work ing. outputcache has a varybycustom parameter, which is used to specify custom dependencies for the cache. When the content changes, the cache will be updated. To use the varybycustom parameter, override the getvarybycustomstring () method of Global. asax. The simple code is as follows:
// Arg is the name of the custom variable passed in by the system. It must be judged below // datacache is a cache class, which is used to access the cache object. When this cache object is updated, the page cache simultaneously updates public override string getvarybycustomstring (httpcontext context, string Arg ){
If (ARG = "uccacheflag ")
{
Object _ flag = datacache. getcache ("uccacheflag"); If (_ flag = NULL) {_ flag = datetime. now. ticks. tostring (); datacache. setcache ("uccacheflag", _ flag, datetime. now. addminutes (commonvalue. cachetime), timespan. zero);} return _ flag. tostring ();} return base. getvarybycustomstring (context, ARG );}
Add the following to the header Declaration of the user control:
<% @ Outputcache duration = "1800" varybycustom = "uccacheflag" %> // uccacheflag is the string determined in getvarybycustomstring ().
You only need to update the cache with the key uccacheflag in the program, and the page output cache will also be updated accordingly. You can judge multiple keywords in getvarybycustomstring () to control the output cache of different user controls.
(4) cache problems in IFRAME The pages embedded in the IFRAME or webpage mode dialog box automatically generate the output cache, so that the internal pages cannot be real-time. This is the cache mechanism provided by IE and is strictly irrelevant to ASP. NET. However, in. net, we can solve this problem through simple code. Add the following code at the beginning of the program. You can set the HTTP request to be updated immediately to prevent ie from caching the page.
Response. expires =-1;
The common cache problems are the above four