How to set cache control headers for AJAX request response

by Admin 22. July 2009 03:03

I have been developing AJAX enabled applications before the term AJAX was coined. I have been doing it for so long that some of things that I do come kind of naturally. And one of the things that I always do is to make sure that response is not cached to ensure that client is never working on stale response even though it sent new request every few seconds. So I have a small piece of code that I pretty much use in all applications that sets some headers.


Response.ContentType = "text/plain";
Response.Expires = -1;
Response.CacheControl = "no-cache";

These are not just the only headers but gives you an idea how cache was being control. I never ran into any trouble with any applications till last week when I was told that our application is filling up Temporary Internet Files folder of the users. This was the first time ever I was reported such issue and actually this was first time I observed this behavior in my applications. So I fired up Fiddler to see whats going on with my requests. I looked at the response headers and saw the following.

First, I was not expecting to see Cache-Control: private. So that was little out of whack. Second, the expiration time was correct because I always set to an hour behind the response time to make sure that it is stale for caching. I have been using the same caching utility routine for so long that I did not suspect that something is wrong there. Then I looked inside Temporary Internet Files folder again and noticed that this was the only request that was being saved in the folder, others were not. So I looked at the implementation and found that the server side implementation for this request was not using my standard utility to set cache headers. Following is the code snippet that I ad in place. Well why i changed the implementation for this particular call is whole different story.


Response.ContentType = "text/plain";
Response.Expires = -1;

Notice that it is missing Cache-control : no-cache header. That explained everything. After I added this header, everything went back to normal. So I decided to do some experiment to observe behavior of setting different headers.

No Cache-control: no-cache header on any call

You will notice that from my earlier post How to serialize multiple AJAX calls in jQuery, I have two AJAX calls being made. And you can see from snapshot above that both are being saved in Temporary Internet File folder.

Cache-control: no-cache header set on one request only

Now you can see that only one request is being saved in the folder and other has disappeared.

Cache-Control:no-cache header set on all requests

Well, there is nothing to show here in Temporary Internet Files folder because nothing is being saved there any more. But here is the snapshot of response headers as seen in Fiddler.

Now you can see that no-cache header and pragma has been set correctly.

Set cache-control header correctly

As more and more applications are using AJAX or Web2.0 style of implementations, if you do not set these cache control headers correctly, you will see that browser cache folders will accumulate lot of entries. It is not that big of a deal as far as application working goes because this temporary cache will not grow beyond specified limits for a particular browser. But it will hurt performance of other internet sites that you visit because their content will not be found in cache and will have to reloaded from server again. Other performance hit you will take is that now browser has to spend an extra CPU cycle to save these entries on the disk.

Views: 11335

Tags:

AJAX | JavaScript | AJAX | JQuery | JQuery | ASP.Net | ASP.Net | JavaScript | JavaScript | JQuery | JQuery

Comments

7/22/2009 3:42:57 AM #

trackback

Setting cache control headers for AJAX response

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

10/12/2009 3:40:35 PM #

personal loans

I never thought of that this way .. good writing.

personal loans United States

11/25/2009 6:27:51 AM #

cash loans

Nice post . keep up the good work

cash loans United States

12/5/2009 6:10:22 AM #

payday loans

I like what I see. keep it going

payday loans United States

12/9/2009 7:29:12 PM #

discount Gucci Shoes

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

discount Gucci Shoes United Kingdom

12/21/2009 7:44:26 AM #

Minnesota Payday Loans

Just try to smile for about 2-3 mins then you can get back to work

Minnesota Payday Loans United States

12/30/2009 9:57:52 AM #

faxless cash advance

Like your writing! Still you can do some things to improve it.

faxless cash advance United States

1/2/2010 4:06:24 AM #

payday loans

I guess there's always an easier way ...

payday loans United States

1/18/2010 1:49:13 PM #

payday loans

Stand up to your obstacles and do something about them. You will find that they haven't half the strength you think they have.

payday loans United States

2/4/2010 5:10:42 AM #

ucvhost

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

ucvhost United States

2/6/2010 7:21:33 AM #

ucvhost

How to use Remote Desktop Connection for the two systems which have same IP Address?

ucvhost United States

2/12/2010 3:07:20 AM #

ucvhost

i. I wanted to drop you a quick note to express my thanks. I've been following your blog for a month or so and have picked up a ton of good information as well as enjoyed the way you've structured your site.

ucvhost United States

3/12/2010 1:58:19 PM #

Adam

I want to suggest this http://soft-net.net/SendHTTPTool.aspx"  free http testing tool  using this tool you can send different cache option and view the Response header from any URL.

Adam Israel

4/6/2010 1:47:36 AM #

Tiffany Keys

Very useful!

Tiffany Keys Malaysia

4/9/2010 12:11:38 PM #

teeth whitening

Fresh activity is the only means of overcoming adversity.

teeth whitening United States

4/23/2010 9:37:21 AM #

fast payday loans

Nothing ever comes to one, that is worth having, except as a result of hard work.

fast payday loans United States

4/25/2010 6:04:21 PM #

vigrx plus

vigrx plus

vigrx plus United States

5/31/2010 9:02:56 PM #

Multivariate Testing

I probably clear my cache about once a month. Is this too much? Or should I be clearing my cache more often? I find my browser is faster when I clear my cache more often, especially after I delete the temporary files.

Multivariate Testing United States

6/21/2010 9:46:13 PM #

Best Life Insurance Companies

You made numerous nice ideas there. I done a search on the issue and learnt nearly all peoples will agree with your blog.

Best Life Insurance Companies Spain

7/6/2010 1:08:47 AM #

coach outlet

Loved to read your article. I would like to suggest you that traffic show most people read articles on Mondays. So it should encourage administer to write new write ups over the weekend primarily.

coach outlet United States

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli