All About Caching In Asp.Net, IIS

Two key factors in improving the speed of your Web applications are:
  • Reducing the number of request/response roundtrips.
  • Reducing the number of bytes transferred between the server and the client.
HTTP caching is of the best ways to reduce roundtrips and bytes transferred. Caching provides a mechanism for a client or proxy to store HTTP responses for later use, so that requests need not cross the network.

For Static Content Caching:-

<System.webServer> <staticContent> <clientCache> :
The <clientCache> element of the <staticContent> element was introduced in IIS 7.0 and <clientCache> element was not modified in IIS 7.5.

The <clientCache> element of the <staticContent> element specifies cache-related HTTP headers that Internet Information Services (IIS) 7 sends to Web clients, which control how Web clients and proxy servers will cache the static content that IIS 7 returns.



<system.webServer>
  <staticContent>
    <clientCache cacheControlMode="UseExpireshttpExpires="Tue, 19 Jan 2018 03:14:07 GMT" />
  </staticContent>

<system.webServer>
 <staticContent>
   <clientCache cacheControlMode="UseMaxAge" cacheControlCustom="public" cacheControlMaxAge="10.00:00:00" />
 </staticContent>


Expires was specified in HTTP 1.0 specification as compared to Cache-Control: max-age, which was introduced in the early HTTP 1.1 specification, if a response includes an Expires header and a max-age directive, the max-age directive, overrides the Expires header.

You can make you custome module to change the setting relating to cache..



void context_EndRequest(object sender, EventArgs e)
{
 HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.Add(new TimeSpan(10, 0, 0, 0, 0)));
 HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
 HttpContext.Current.Response.Cache.SetMaxAge(new TimeSpan(10, 0, 0, 0, 0));
 HttpContext.Current.Response.Cache.SetLastModifiedFromFileDependencies();
 HttpContext.Current.Response.Cache.SetETagFromFileDependencies();
 //HttpContext.Current.Response.Cache.SetETag("000");
}



Rendered Response Header:
Cache-Control: public, max-age=36000
Expires: Tue, 19 Jan 2018 03:14:07 GMT
Etag: "329e553765acd1:0"
Last-Modified: Thu, 05 Jul 2012 06:20:50 GMT

Etag and Last-Modified date :
An ETag or entity tag is part of HTTP, the protocol for the World Wide Web. It is one of several mechanisms that HTTP provides for web cache validation, and which allows a client to make conditional requests.

An Entity Tag is a validator which can be used instead of, or in addition to, the Last-Modified header. An entity tag is a quoted string which can be used to identify different versions of a particular resource.

Every time when browser makes a request for a resource server check the request match the etag if there is no chance then it returns (http status code 304) 304 not modified status just use content from your cache else return (http status 200 ok) just cache it  (if expire header or cache control max age exist in response header)

Response Header :
Cache-Control: public, max-age=36000
Expires: Tue, 19 Jan 2018 03:14:07 GMT
Etag: "329e553765acd1:0"
Last-Modified: Thu, 05 Jul 2012 06:20:50 GMT

Instructions :
  1. Don’t use etag / last-moidfied headers for images.
  2. Don’t use etag / last-modified headers for an application which hosted in multi server(using load balancing).
  3. Use long expires date in Expires header with etag / last-modified headers if your content frequently changes (in a short amount of time).
  4. Use short expires date (Average time) in expires header with no etag / last-modified headers if you don’t know when your content changes.
  5. Use estimate amount of time in expires header with no etag / last-modified headers if you know the estimate time of content when to change.
More Details:

For Dynamic Content Caching (OutPut Caching) : -

<System.web> <Caching>:-

The <caching> element allows you to enable or disable page output caching for an Internet Information Services (IIS) 5, (IIS) 6 applications.





<system.web>
  <caching>
    <outputCache enableOutputCache="true"  />
     <outputCacheSettings>
       <outputCacheProfiles>
         <add location="Any" duration="14800" enabled="truevaryByParam="*name="AssetCacheProfile" />
       </outputCacheProfiles>
     </outputCacheSettings>
   </caching>


<System.WebServer><Caching>:-

The <caching> element was introduced in IIS 7.0 and <caching> element was not modified in IIS 7.5, the setting of this area overrides all previous (<system.web><caching>) tag settings.
                                                                
The <caching> element allows you to enable or disable page output caching for an Internet Information Services (IIS) 7 application. This element also allows you to configure whether IIS caches page output in user mode, kernel mode, or both and what, if any, output caching limits you want to impose.

The <caching> element also contains a <profiles> element that contains a collection of output cache settings that you can apply to ASP.NET pages.



<system.webServer>
 <caching enabled="true" enableKernelCache="true">
   <profiles>
     <add extension=".asp" policy="CacheUntilChange"
        kernelCachePolicy="CacheUntilChange" duration="00:30:00varyByQueryString="Id, Name" />
   </profiles>
 </caching>


The IIS 7 output cache supports two cache policies:

  • User-mode output cache policy, which uses a cache that resides in an IIS 7 worker process.
  • Kernel-mode cache policy, which uses a cache that resides in Http.sys, a kernel-mode driver.
Default Value :
By Default Output caching is (Enable caching , Enable kernel) true in iis 7 or latter.

More Details:-

Dynamic Script (ScriptManager Script):
Dynamic script such as .axd handled by scriptResourceHandler by default enableCompression and enableCaching for scriptmanager scripts (.axd) is true if you need not to compress or cache these resource just set false value.



<system.web.extensions>
    <scripting>
      <scriptResourceHandler enableCompression="trueenableCaching="truemaxResponseSize="512000" />
    </scripting>
</system.web.extensions>


Popular Posts