Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [396]

By Root 5717 0
In general, it can go in three different locations, one not necessarily excluding the other. The page can be cached on the client (the browser cache), on the IIS Web server, and even by an intermediate proxy server. The various options are listed in Table 18-9. They come from the OutputCacheLocation enumerated type.

Table 18-9. Output Cache Locations

Location

Cache-Control

Expires

Description

Any

Public

Set according to the value of the duration attribute.

The page is cached everywhere, in the browser as well as in any intermediate proxy. In addition, it is also cached on the Web server according to the current output cache provider.

Client

Private

Set according to the value of the duration attribute.

The page is cached only on the browser. It is ignored by any intermediate proxy, and it is not processed by any output cache provider registered in the ASP.NET application.

DownStream

Public

Set according to the value of the duration attribute.

The page can be cached on the browser and by any intermediate cache-enabled proxies. It won’t be processed by any output cache provider registered in the ASP.NET application.

None

No-Cache

– 1

Also, the Pragma header is set to No-Cache. As a result, the page is never served from any cache.

Server

No-Cache

– 1

Also, the Pragma header is set to No-Cache. The page is cached only by the output cache provider currently registered in the ASP.NET application.

ServerAndClient

Private

Set according to the value of the duration attribute.

The page is cached on the browser, and it is also processed by the output cache provider currently registered in the ASP.NET application. It will be ignored by any proxy in the middle.

When the cache-control header is public, ASP.NET also emits the header max-age set to the same value as the duration attribute. Expires and max-age play the same role except that the former requires an absolute date and time (that has to be parsed by browsers and proxies), whereas the latter just indicates the number of seconds to wait. In general, when both Expires and max-age are specified, max-age wins.

The value of No-Cache assigned to the cache-control HTTP header instructs the browser to check with the server as to the freshness of the page before serving it. However, in combination with Expires set to –1, it indicates that the page is stale and subsequently needs be refetched. The net effect is the same as if the page was never cached. The No-Store value, on the other hand, instructs the browser not to save the resource locally. If the page comes over HTTPS, however, it will never be cached locally.

In addition to browser and proxy caches, I mentioned server-side caching. I’ll return to that in a moment; for now, it suffices to say that it is yet another level of page-output caching specific to ASP.NET. A special component—the page output provider—will capture the output of a page and cache it somewhere on the server. The default provider caches pages inside the ASP.NET Cache object in the memory space of the worker process (and machine) that is currently serving the request. As you can see, this solution is not ideal if you have a Web farm where there’s no guarantee that subsequent requests for the same page are served by the same machine. If you’re running a Web farm, you might want to consider replacing the default provider with the output cache provider made available by the AppFabric Caching Services.

Choosing a Duration for ASP.NET Page Output


When the output caching service is active on a page, the Duration attribute indicates the number of seconds that the caching system will maintain an HTML-compiled version of the page. Next, requests for the same page, or for an existing parameterized version of the page, will be serviced while bypassing most of the ASP.NET pipeline. As mentioned, this process has two important repercussions—no authentication is possible and no code is run, meaning that no page events are fired and handled and no state is updated.

A fair value for the Duration attribute depends on the application.

Return Main Page Previous Page Next Page

®Online Book Reader