Analysis and Practice of ASP. NET Cache

Source: Internet
Author: User

When it comes to ASP. NET caching, that is: cache as early as possible; cache should be implemented at every layer of the application. Add cache support to the data layer, business logic layer, UI, or output layer. Memory is currently very cheap-therefore, implementing caching in the entire application in a smart way can greatly improve performance. Caching can mask many errors. caching is a way to achieve "good enough" performance without having to spend a lot of time and analysis.

The memory is very cheap now, so if you can cache the output for 30 seconds, instead of spending a whole day or even a week trying to optimize the code or database to get the required performance, you will certainly choose a cache solution that can accept 30 seconds of old data ). Cache is one of the features that get a 20% return Using 80%. Therefore, to improve performance, you should first think of cache.

However, if the design is poor, it may lead to adverse consequences. Therefore, you should, of course, try to design the application correctly. However, if you only need to obtain high performance immediately, the cache is your best choice. You can redesign the application as soon as you have time. The page-level output cache is the simplest form of cache. The output cache only retains the HTML copies sent in response to the request in the memory. When there are other requests, the cache output will be provided until the cache expires. In this way, the performance may be greatly improved depending on the overhead required to create the original page output-the output of the sending cache is always fast and stable ). To implement the page output cache, you only need to add an OutputCache command to the page.

<%@ OutputCache Duration = "60" VaryByParam = "*" %>

Like other page commands, this command should appear at the top of the ASPX page, that is, before any output. It supports five attributes or parameters), two of which are required. 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 Any, Client, Downstream, None, Server, or ServerAndClient. Required attribute of VaryByParam. The name of the variable in the Request. These variable names should generate separate cache entries. "None" indicates no change. "*" Can be used to create a cache entry for each variable group. Variables are separated. VaryByHeader changes cache entries based on changes in the specified header.

VaryByCustom allows you to specify custom changes in global. asax, for example, "Browser "). The combination of the required Duration and VaryByParam options can be used to handle most cases. For example, if your product directory allows users to view the directory Page Based on categoryID and page variables, you can use the parameter value to "categoryID; page "VaryByParam caches the product directory for a period of time. If the product is not changed at any time, it is acceptable for one hour. Therefore, the duration is 3600 seconds ). This creates separate cache entries for each directory page of each category. Each entry is counted from its first request for one hour. VaryByHeader and VaryByCustom are mainly used to customize the appearance or content of the page based on the client accessing the page. The same URL may need to be rendered and output for both the browser and mobile client. Therefore, different content versions must be cached for different clients.

Or, the page may have been optimized for IE, but it needs to be completely optimized for Netscape or Opera, not just to destroy the page ). The next example is very common. We will provide an example to illustrate how to achieve this goal: Example: VaryByCustom is used to support browser customization so that each browser has a separate cache entry, the value of VaryByCustom can be set to "browser ". This function has been built into the cache module and will insert a separate page Cache version for each browser name and major version.

<%@ OutputCache Duration = "60" VaryByParam = "None" VaryByCustom = "browser" %>

Fragment Caching: the user control outputs the cache and caches the entire page. This is usually not feasible because some parts of the page are customized for users. However, the rest of the page is shared by the entire application. These parts are most suitable for cache using fragment caching and user controls. Menu and other layout elements, especially those dynamically generated from the data source, should also be cached in this way. If necessary, you can configure the cached controls to be changed based on changes to their controls or other properties, or any other changes supported by the page-level output cache. Using hundreds of pages of the same control group can also share the cache entries of those controls, rather than retaining a separate Cache version for each page. The syntax used to implement fragment caching is the same as that used for page-level output caching, but it is applied to the user control. ascx file) instead of the Web form. aspx file ). In addition to the Location attribute, user controls also support all attributes supported by OutputCache on Web forms. The user control also supports the OutputCache attribute named VaryByControl. This attribute changes the control's cache based on the value of a member of a user control, such as DropDownList.

If VaryByControl is specified, You can omit VaryByParam. Finally, by default, each user control on each page is cached separately. However, if a user control does not change with the page in the application and the same name is used on all pages, you can apply the Shared = "true" parameter, this parameter allows the cached version of the user control to be used by all pages that reference the control. Example

<% @ OutputCache Duration = "60" VaryByParam = "*" %> in this example, ASP.. NET caches the user control for 60 seconds, and creates separate cache entries for each change of the query string and for each page where the control is located.

<%@ OutputCache Duration = "60" VaryByParam = "none" VaryByControl = "CategoryDropDownList" %>

In this example, the ASP. NET user control is cached for 60 seconds, and a separate cache entry is created for each different value of the CategoryDropDownList control and for each page where the control is located.

<%@ OutputCache Duration = "60" VaryByParam = "none" VaryByCustom = "browser" Shared = "true %>

Finally, this example caches the ASP. NET user control for 60 seconds and creates a cache entry for each browser name and major version. Then, the cache entries of Each browser will be shared by all pages that reference this user control, as long as all pages reference this control with the same ID ). Page-level and user control-level output cache is indeed a way to quickly and easily improve site performance, but ASP. the true flexibility and powerful functions of the NET Cache are provided by the Cache object. With Cache objects, you can store any serializable Data Objects and control the expiration method of Cache entries based on the combination of one or more dependencies. These dependencies can include the time since the item was cached, the time since the item was last accessed, changes to files and/or folders, and changes to other cache items, after slight processing, you can also include changes to specific tables in the database.

The method analysis and instance explanation of ASP. NET cache is here. I hope it will help you understand ASP. NET cache.

  1. Introduction to ASP. NET page framework
  2. Analysis of Application Exception Handling in ASP. NET project development
  3. Common Exception Handling and web. config Configuration Analysis in ASP. NET project development
  4. Analysis of health monitoring in ASP. NET project development
  5. Introduction to ASP. NET cache and Its Application

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.