Skip to main content

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 from this blog

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview , Scrollble gridview with fixed header through javascript , File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview. We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.