ASP. Net Common cache updates

Source: Internet
Author: User
ASP. net to update the page output cache (including user controls, iframe, page cache, etc.) ASP. the built-in cache mechanism of 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 for programmers to quickly update cached data. 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 CacheCache objects provide code-level caching, Which is powerful and operable. 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 OutputCacheCompared 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 OutputCacheThe 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, page cache updated at the same time
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 iframeThe 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. If you have a better method, please leave a message here. 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 for programmers to quickly update cached data. 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 CacheCache objects provide code-level caching, Which is powerful and operable. 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 OutputCacheCompared 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 OutputCacheThe 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, page cache updated at the same time
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 iframeThe 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. If you have a better method, please leave a message here.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.