Early cache; frequent Cache
You should implement caching at each 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 mistakes
Caching is a way to achieve "good enough" performance without much time and analysis. The memory is very cheap now, so if you can Cache
Second, instead of spending a whole day or even a week trying to optimize the code or database to get the required performance, you will definitely choose a cache solution (assuming you can accept 30
Seconds ). The cache is the one that uses 20% to get 80%
Therefore, to improve performance, you should first consider caching. 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.
Page-level output Cache
As the simplest form of caching, 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, 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 ).
Implementation
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
Specify the location where the output should be cached. To specify this parameter, it must be any, client, downstream, none, server, or serverandclient.
Varybyparam
Required attribute. 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.
VarybycustomAllowed in global. asax
For example, "Browser "). Use required duration and varybyparam
The combination of options can 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
Varybyparam of "categoryid; page"
The product directory is cached 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 is mainly used to customize the appearance or content of the page based on the client accessing the page. Same URL
The browser and mobile phone client may need to be rendered and output at the same time. Therefore, different content versions must be cached for different clients. Or, the page may have been targeted at IE
Optimized, but for Netscape or opera
Completely reduce optimization (not just damage 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.
To make each browser have 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" %>
Segment cache, user control output Cache
Caching the entire page 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 using fragment caching and user controls.
Cache. Menu and other layout elements, especially those dynamically generated from the data source, should also be cached in this way. If needed, you can configure the cached control as based on its control (or other attributes)
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.
ImplementationThe syntax used by fragment caching is the same as that used by page-level output caching, but it is applied to user controls (. ascx files) instead of Web
Form (. aspx file ). Besides the location attribute
All the properties supported on the form are also supported by user controls. The user control also supports outputcache named varybycontrol.
Property, which changes the cache of the control based on the value of the member of the user control (usually the control on the page, for example, dropdownlist. If
Varybycontrol, which can be omitted
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
If the page uses the same name, 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, the user control is cached for 60 seconds, and a separate cache entry is created for each change of the query string and for each page where the control is located.
<% @ Outputcache duration = "60" varybyparam = "NONE" varybycontrol = "categorydropdownlist" %>
This example caches the user control for 60 seconds, and creates separate cache entries 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 user control 60
And a cache entry will be created 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 use the same
ID to reference the control ).
Page-level and user control-level output cache is indeed a way to quickly and easily improve the performance of the site, but in ASP. NET, the true flexibility and powerful functions of cache are through
Provided by the cache object. Use 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 after the item is cached
Changes to files and/or folders, and changes to other cache items after the next access can also include changes to specific tables in the database.