Use URLs
Consistently
Browsers cache content based on the URL. when URL changes, browser fetches a new version from origin server. URL can be changed by changing the query string parameters. for example, "/default. aspx "is cached on the browser. if you request "/default. aspx? 123 "It will fetch new content from server. response from the new URL can also be cached in browser if you return proper caching headers. in that case, changing the query parameter to something else like "/default. aspx? 456 "will return new content from server. so, you need to make sure you use URL consistently everywhere when you want to get cached response. from homepage, if you have requested a file with URL "/welcome.gif", make sure from another page you request the same file using the same URL. one common mistake is to sometimes omit the "www" subdomain from the URL. www.pageflakes.com/default.aspx is not same as pageflakes.com/default.aspx. both will be cached separately.
Cache static content for longer period
Static files can be cached for longer period like one month. if you are thinking that you shoshould cache for couple of days so that when you change the file, users will pick it up sooner, you're mistaken. if you update a file which was cached by Expires header, new users will immediately get the new file while old users will see the old content until it expires on their browser. so, as long as you are using Expires header to cache static files, you shoshould use as high value as possible.
For example, if you have set Expires header to cache a file for three days, one user will get the file today and store it in cache for next three days. another user will get the file tomorrow and cache it for three days after tomorrow. if you change the file on the day after tomorrow, the first user will see it on fourth day and the second user will see it on every th day. so, different users will see different versions of the file. as a result, it does not help setting a lower value assuming all users will pick up the latest file soon. you will have to change the URL of the file in order to ensure everyone gets the exact same file immediately.
You can setup Expires header from static files from IIS manager. You'll learn how to do it in later section.
Use cache friendly folder structure
Store cached content under a common folder. for example, store all images of your site under the "/static" folder instead of storing images separately under different subfolders. this will help you use consistent URL throughout the site because from anywhere you can use "/static/images/somefile.gif ". later on, we will learn it's easier to move to a content delivery network when you have static cacheable files under a common root folder.
Reuse common graphics files
Sometimes we put common graphics files under several virtual directories so that we can write smaller paths. for example, say you have indicator.gif in root folder, some subfolders and Under CSS folder. you did it because you need not worry about paths from different places and you coshould just use the file name as relative URL. this does not help in caching. each copy of the file is cached in browser separately. so, you shoshould collect all graphics files in the whole solution and put them under the same root "static" folder after eliminating duplicates and use the same URL from all the pages and CSS files.
Change file name when you want to expire Cache
When you want a static file to be changed, don't just update the file because it's already cached in user's browser. you need to change the file name and update all references everywhere so that browser downloads the new file. you can also store the file names in database or configuration files and use data binding to generate the URL dynamically. this way you can change the URL from one place and have the whole site receive the change immediately.
Use a version number while accessing static files
If you do not want to clutter your static folder with multiple copies of the same file, you can use query string to differentiate versions of same file. for example, a GIF can be accessed with a dummy query string like "/static/images/indicator.gif? V = 1 ". When you change the indicator.gif, You can overwrite the same file and then update all references to the file to"/static/images/indicator.gif? V = 2 ". This way you can keep changing the same file again and just update the references to access the graphics using the new version number.
Store cacheable files in a different domain
It's always a good idea to put static contents in a different domain. first of all, browser can open another two concurrent connections to download the static files. another benefit is that you don't need to send the cookies to the static files. when you put the static files on the same domain as your web application, browser sends all the ASP. net cookies and all other cookies that your web application is producing. this makes the request headers be unnecessarily large and waste bandwidth. you don't need to send these cookies to access the static files. so, if you put the static files in a different domain, those cookies will not be sent. for example, put your static files in www.staticcontent.com domain while your website is running on www.dropthings.com. the other domain does not need to be a completely different web site. it can just be an alias and share the same Web Application Path.
SSL is not cached, so minimize SSL use
Any content that is served over SSL is not cached. so, you need to put static content outside SSL. moreover, you shocould try limiting SSL to only secure pages like login page or payment page. rest of the site shocould be outside SSL over regular HTTP. SSL encrypts request and response and thus puts extra load on server. encrypted content is also larger than the original content and thus takes more bandwidth.
Http post requests are never cached
Cache only happens for http get requests. http post requests are never cached. So, any kind of Ajax call you want To make cacheable, it needs to be http get enabled.
Generate Content-Length Response Header
When you are dynamically serving content via web service CILS or HTTP handlers, make sure you emitContent-LengthHeader. browsers have several optimizations for downloading contents faster when it knows how many bytes to download from the response by looking atContent-LengthHeader. browsers can use persisted connections more than tively when this header is present. this saves browser from opening a new connection for each request. when there's no Content-Length header, browser doesn't know how many bytes it's going to receive from the server and thus keeps the connection open as long as it gets bytes delivered from the server until the connection closes. so, you miss the benefit of persisted connections that can greatly reduce download time of several small files like CSS, javascripts, and images.
How to configure static content caching in IIS
In IIS manager, web site Properties dialog box has "HTTP headers" tab where you can define Expires header for all requests that IIS handles. there you can define whether to expire content immediately or expire after certain number of days or on a specific date. the second option (expire after) uses sliding expiration, not absolute expiration. this is very useful because it works per request. whenever someone requests a static file, IIS will calculate the expiration date based on the number of days/months from the expire after.
For dynamic pages that are served by ASP. NET, a handler can modify the Expires header and override IIS default setting.