ASP.NET OutputCache Details

  • 2021-06-28 12:19:53
  • OfStack

When a user accesses a page, the entire page is saved in memory by the server, which caches the page.When the user visits the page again, the page does not perform data operations again. The page first checks for the presence of a cache in the server. If the cache exists, the page information is retrieved directly from the cache. If the page does not exist, the cache is created.

Page output caching is appropriate for pages with large amounts of data and not too many event operations. Page output caching cannot be used if a page needs to perform a large number of event updates and data updates.The @OutputCatch directive enables you to declare a page output cache, as shown in the sample code below.


<%@ OutputCache Duration="120" VaryByParam="none" %>

The above code declares a page cache using the @OutputCatch directive, which will be cached for 120 seconds. @The OutputCatch directive contains 10 attributes that allow you to cache pages for different situations. The common attributes are as follows:

CacheProfile: Gets or sets the OutputCacheProfile name.

Duration: Getting or setting the time a cache item needs to remain in the cache.

VaryByHeader: Gets or sets a well-separated set of HTTP header names used to change cached items.

Location: Gets or sets a value that determines the location of the cache entry, including Any, Clint, Downstream, None, Server, and ServerAndClient.The default value is Any.

VaryByControl: Gets or sets a cluster of delimited control identifiers contained within the current page or user control to change the current cache entry.

NoStore: Gets or sets a value that determines whether the "Http Cache-Control:no-store" directive is set.

VaryByCustom: Gets a list of custom strings used by the output cache to change cache entries.

Enabled: Gets or sets a value indicating whether output caching is enabled for the current content.

VaryByParam: Gets a list of query strings or form POST parameters.

By setting the appropriate properties, you can set the appropriate cache for the page. When you need to set a cache entry for Default.aspx, you can use the VaryByParam property to set it, as shown in the sample code below.


<%@ OutputCache Duration="120" VaryByParam="none" %>

The code above uses the Duration and VarByParam properties to set the cache properties of the current page.Overall caching settings for a page are often unnecessary and often confusing, such as Default.aspx?id=1 and Default.aspx ?id=100 may render the same page when cached, which is often not what developers want.The caching parameters can be specified by configuring the VarByParam property, as shown in the sample code below.

<%@ OutputCache Duration="120" VaryByParam="id" %>

The above code is cached by the parameter id, and ASP.NET caches pages differently when id items are different.This guarantees Default.aspx?id=1 and Default.aspx?id=100 does not display the same page when cached.VarByHeader and VarByCustom are mainly used to customize the appearance or content of a page based on the client accessing the page.In ASP.NET, a page may need to render output to PC and MOBILE users, so different data can be cached by different versions of the client, as shown in the sample code below.

<%@ OutputCache Duration="120" VaryByParam="none" VaryByCustom="browser" %>

The above code sets up a separate cache entry for each browser.


Related articles: