Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

ASP.NET MVC and HTTP/1.1 Caching

Tags: asp.net, http, caching

Overview

  • ASP.NET provides two caching mechanisms:
    • output caching, which stores files in “any HTTP 1.1 cache-capable device” from the originating server to the client browser.
    • application data caching, which stores data objects in server memory (i.e. in server RAM).
  • We want output caching, because it is related to HTTP Caching.

HTTP Cache Control Mechanisms

HTTP headers exert influence over what HTTP caches store and for how long.

Cache Related Headers.

There are at least two deprecated headers. These are still useful when we want to support HTTP/1.0 or older browser versions. Other cache related headers are useful for calculating expiration.

Table: Cache Related HTTP headers and corresponding ASP.NET MVC OutputCacheAttribute property values.

Cache Related HTTP Header Corresponding ASP.NET MVC OutputCacheAttribute Property Values
Expires  
Pragma: no-cache  

Common Cache-Control Header Directives.

The Cache-Control header can have many directive values. The directives work for HTTP/1.1 cache-capable devices and are part of the ASP.NET MVC OutputCacheAttribute.

Table: HTTP Cache-Control directives and corresponding ASP.NET MVC OutputCacheAttribute properties

HTTP Cache-Control Directive Corresponding ASP.NET MVC OutputCacheAttribute Property Values
max-age Duration = Milliseconds
no-store NoStore = true
private Location = OutputCacheLocation.Client | OutputCacheLocation.ServerAndClient
public Location = OutputCacheLocation.Any | OutputCacheLocation.Downstream
no-cache Location = OutputCacheLocation.None | OutputCacheLocation.Server
must-revalidate ?

ASP.NET MVC Default for Location = .

The default is Location = OutputCacheLocation.Any, which should NOT cache personal information such as username, because it goes into a public cache location.

Where to Set Cache-Control?

OutputCache Attribute.

  • Add the OutputCache attribute to Controllers or Actions.
  • If you add the attribute to a controller, it affects all of the controller’s actions.

OutputCache Properties.

  • Add OutputCache properties to the OutputCache attribute in code, or
  • to an output cache profile in web.config > configuration > system.web > caching > outputCacheSettings >  outputCacheProfiles > add
  <system.web>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add
            name="TransparentClient"
            duration="0"
            location="Client"
            noStore="true" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>

Research Questions

  • How do we implement application wide HTTP Cache-Control directives?
  • Where in an ASP.NET MVC application can we configure Cache-Control directives?
  • How do we implement common HTTP Cache-Control header directives?
  • How do we implement deprecated cache-related headers?

References