ASP. NET OutputCache
This article describes ASP. NET OutputCache in detail. This article describes the OutputCache syntax, OutputCache parameters, and OutputCache usage examples. For more information, see
When a user accesses a page, the whole page will be stored in the memory by the server, so that the page is cached. When a user visits this page again, the page does not perform data operations again. The page first checks whether the cache exists on the server. If the cache exists, the page information is directly obtained from the cache, if the page does not exist, a cache is created.
The page output cache is applicable to pages with a large amount of data and no excessive event operations. If a page requires a large number of event updates and data updates, the page output cache cannot be used. Use the @ OutputCatch command to declare the page output cache. The sample code is as follows.
The Code is as follows:
<% @ OutputCache Duration = "120" VaryByParam = "none" %>
The above Code uses the @ OutputCatch command to declare the page cache. The page will be cached for 120 seconds. The @ OutputCatch command contains 10 attributes. These attributes can be used to set cache for different page conditions. common attributes are as follows:
CacheProfile: gets or sets the OutputCacheProfile name.
Duration: gets or sets the time that the cache entry needs to be retained in the cache.
VaryByHeader: gets or sets the names of all well-separated HTTP headers used to change cache items.
Location: gets or sets a value that determines the Location of the cache item, including Any, Clint, Downstream, None, Server, and ServerAndClient. The default value is Any.
VaryByControl: gets or sets a cluster of control identifiers separated by commas (,). These identifiers are contained in the current page or user control and used to change the current cache items.
NoStore: gets or sets a value to determine whether the "Http Cache-Control: no-store" command is set.
VaryByCustom: gets a list of custom strings that the output cache uses to change cache items.
Enabled: gets or sets a value indicating whether the output cache is Enabled for the current content.
VaryByParam: gets the list of query strings or form POST parameters.
You can set the corresponding cache for the page by setting corresponding properties. When you need to set cache items for Default. aspx, you can use the VaryByParam attribute to set the cache items. The sample code is as follows.
The Code is as follows:
<% @ OutputCache Duration = "120" VaryByParam = "none" %>
The preceding Code uses the Duration Attribute and VarByParam attribute to set the cache attribute of the current page. It is often unnecessary to set the overall cache for a page, which may also cause problems, such as Default. aspx? Id = 1 and Default. aspx? Id = 100 the pages that may be displayed in the cache are the same, which is often not desired by developers. You can specify cache parameters by configuring the VarByParam attribute. The sample code is as follows.
The Code is as follows:
<% @ OutputCache Duration = "120" VaryByParam = "id" %>
The above code is cached by the parameter id. When the id items are different, the pages cached by ASP. NET are also different. This ensures Default. aspx? Id = 1 and Default. aspx? The page displayed when id = 100 is cached is inconsistent. VarByHeader and VarByCustom are mainly used to customize the appearance or content of the page based on the client accessing the page. In ASP. NET, a page may need to be output to PC users and MOBILE users. Therefore, different data can be cached using different client versions. The sample code is as follows.
The Code is as follows:
<% @ OutputCache Duration = "120" VaryByParam = "none" VaryByCustom = "browser" %>
The above Code sets a separate cache entry for each browser.