The HTTP caching specification

The HTTP 1.1 caching specification (https://tools.ietf.org/html/rfc7234) describes how caches should behave over HTTP. The main header related to HTTP caching is the Cache-Control header. This header is used to specify directives in the request and response. It is also essential to note that the Cache-Control directives that are defined in the request and the response are independent. The following diagram shows a typical request-response workflow:

The schema describes the interaction between a generic client and a server with a caching layer between them.

First, the client requests a resource from the server, and the caching layer forwards it to the server. The server produces the data for the client and sends the response back; at this point, the caching server caches the response in the caching layer. Therefore, if the client performs another call to the same content, the request will not hit the server, but the cache system will provide the cached response.

It is essential to note that the cache layer adds some headers to the response:

Cache-Control: max-age=100
Age:0

Both of these are HTTP headers related to the caching directives. Cache-Control adds the max-age directive to indicate that the freshness lifetime of the content is equal to 100. The Age header specifies the age of the cached content. When Age reaches the Cache-Control: max-age value, the caching layer will forward the request to the server in order to serve the fresh data. Both of the values of the headers are specified in seconds.

The Cache-Control header can be used to specify the caching mechanism. By default, it is possible to disable the cache by specifying the no-cache directive, that is, Cache-Control: no-cache. Another crucial aspect of the Cache-Control header is the public and private directives, such as Cache-Control: public,max-age=100. The public instruction means that the cached response can also be stored in a shared cache, and any other client can access that information. On the other hand, when a response is private, this means that it probably contains sensitive information and it cannot be shared with other clients.

The caching specification also defines the Vary header. This kind of header is used to indicate which fields influence the cache variation. More specifically, it is used to decide whether a cached response can be used rather than requesting a fresh one from the original server:

Vary: *
Vary: User-Agent

In the first line of the preceding code, each variation in the request is treated as a single and uncacheable request. In the second line, the request is processed as uncacheable, but the User-Agent header is added.

The Expires header has the same purpose as the max-age directive: to give the cache expiration time. The only reason they differ is that the max-age instruction focuses on a fixed date time. For example, we can set the following value:

Expires: Wed, 21 Oct 2015 07:28:00 GMT

It is essential to note that the max-age directive overrides the Expires header. Therefore, if both of them are present in the same response, the Expires header is ignored. In the next section, we will learn how to implement response caching using ASP.NET Core tools.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.6