One way to manage page caching in asp.net is a declarative way with the <% @ OutputCache%> directive. Below is a description and study of the work of this directive. Most of the information is taken from msdn, but this is not a translation, but rather a summary. The text also contains my own research on how a particular parameter affects http-headers.
A directive is used to specify page caching or user controls.
<% @ OutputCache%> . It is usually specified after the Page or Control directive.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResumeList.aspx.cs" Inherits="services_vacancies_ResumeList" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>
<% @ OutputCache%> has several parameters:
Cacheprofile
CacheProfile - this parameter is optional and defaults to an empty string. This parameter cannot be used in .ascx files. CacheProfile is used to specify the caching profile specified via web.config and the caching section.
Example:
The web.config defines the caching section.
< caching >
< outputCacheSettings >
< outputCacheProfiles >
< add name = "Cache30Seconds" duration = "30"
varyByParam = "none" />
</ outputCacheProfiles >
</ outputCacheSettings >
</ caching >
* This source code was highlighted with Source Code Highlighter .
This section is used declaratively on the page, via CacheProfile
<% @ OutputCache CacheProfile = "Cache30Seconds"%>
')
CacheProfile is convenient to use when there are several well-established caching rules for pages on the site. It makes sense to put such rules into the profile of the configuration file, and on the page only specify a link to this profile.
Duration
Duration is a required parameter. Duration sets the time value in seconds during which the page or user element is cached. In fact, this parameter determines the value of the max-age parameter in the http-header of the server response for the Cache-Control field:
Cache-Control: public, max-age = 60
Location
Location — This parameter describes the rule for the cache's storage location and takes one of the values of the OutputCacheLocation enumeration. This parameter cannot be used in .ascx files. By default, Location is set to OutputCacheLocation.Any. All possible Location values are listed below:
Any
the cache can be stored everywhere, including at the client, on the server, on intermediate proxy servers. This value defines the Cache-Control HTTP header as public.
Client
the cache is stored on the client side. This value defines the Cache-Control HTTP header as private.
Downstream
The cache can be stored on any http 1.1-compatible device, including the proxy server or the client, but not on the server. In fact, in the header will be the following information:
Cache-Control: public
Server
the cache is stored only on the server where the request was processed. This value identifies the Cache-Control HTTP header as server. In fact, after specifying Location = "Server", the following information will be in the header:
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
None
This value indicates that the page or user element cannot be cached. This value identifies the Cache-Control HTTP header as nocache. The generated header will be the same as when using Server.
Serverandclient
the cache can only be stored on the server or on clients, proxy servers should not cache the contents. This value defines the Cache-Control HTTP header as a combination of private and server values. In fact, the title will contain:
Cache-Control: private
Vary: *
NoStore
NoStore accepts a boolean value. If true, adds the no-store parameter to the Cache-Control http-header directive. This parameter cannot be used in .ascx files. This option can be useful for preventing page caching in firefox (described here
aspadvice.com/blogs/rjdudley/archive/2006/04/07/16280.aspx ).
Shared
Shared - this parameter takes a boolean value and indicates that the user control can be shared with different pages. Default Shared = ”false”. This parameter cannot be used in .aspx files. In fact, the inclusion of this parameter means that any page using the ascx file will be able to retrieve the saved data from one cache. By default, the same ascx files for each page are saved in different places. In most cases, enabling this option will allow you to save memory on the server.
SqlDependency
SqlDependency - may contain a string of “database: table” pairs on which caching depends.
<% @ OutputCache Duration = "3600" VaryByParam = "None" SqlDependency = "Northwind: Products; Northwind: Categories"%>
It is assumed that when the specified tables are updated, the elements (page or user control) are removed from the cache. SqlDependency for aspx only can contain the value CommandNotification, then you need to register a notification through the SqlDependency class.
VaryByContentEncoding
VaryByContentEncoding specifies a caching condition depending on the contents of the Accept-Encoding http-header directive. The following example will allow caching pages compressed with gzip only:
<% @ OutputCache Duration = "60" VaryByParam = "none" VaryByContentEncoding = "gzip"%>
VaryByControl
VaryByControl - specifies a string that contains a semicolon-separated list of control IDs to be cached on the server. For the OutputCache directive, this attribute is mandatory or VaryByParam. This attribute adds the parameter Vary to the http header: *.
<% @ OutputCache Duration = "60" VaryByControl = "AddResumeCtrl; LoggerCtrl"%>
VaryByCustom
VaryByCustom - any text to control caching. If this text is equal to “browser”, then caching will be done conditionally by the name of the browser and its major version. If a string is specified for VaryByCustom, then you must override the GetVaryByCustomString method in the Global.asax file to implement conditional caching.
VaryByHeader
VaryByHeader is a string with semicolon-separated values of http-headers by which conditional caching will be performed. This parameter cannot be used in .ascx files. Adds the vary parameter to the http header with the corresponding value. For example, the following code:
<% @ OutputCache Duration = "30" VaryByParam = "none" VaryByHeader = "Referer"%>
will add to the http-response header:
Vary: Referer
but only when paired with Location = "Downstream" or Location = "Any".
VaryByParam
VaryByParam - sets conditional caching based on the values of the query string during GET or parameters during POST. Values must be separated by a semicolon. It seems that conditional caching occurs only on the server, because regardless of the value of VaryByParam in the Vary http-header is "*". VaryByParam - required to be specified if the VaryByControl attribute is not specified. VaryByParam can be equal to “none” and then conditional on caching parameters does not occur, or VaryByParam can be equal to “*” and then conditional caching is performed on all parameters.