Atlas 7: Caching web service response on browser and save bandwidth significantly

Browser can cache images, javascripts, css files on users hard drive and it can also cache Xml Http calls if the call is a Http Get. The cache is based on Url. If it's the same Url and it's cached on the computer then the response is loaded from cache, not from the server when it is requested again. Basically, browser can cache any Http Get call and return cached data based on Url. If you make a Xml Http call as Http GET and server returns some special header which informs the browser to cache the response; on future calls, the response will be immediately returned from the cache and thus save the delay of network roundtrip and download time.

At Pageflakes, we cache user's state so that when user visits again the following day, user gets a cached page which loads instantly from browser cache, not from the server. Thus second time load becomes very fast. We also cache several small parts of the page which appears on users action. When user does the same action again, a cached result is loaded immediately from local cache and thus saves the network roundtrip time. User gets a fast loading site and a very responsive site. The perceived speed increases dramatically.

The idea is to make Http Get calls while making Atlas web service calls and return some specific Http Response headers which tells the browser to cache the response for some specific duration. If you return "Expires" header during the response, browser will cache the Xml Http response. There are 2 headers that you need to return with response which will instruct browser to cache the response:

HTTP/ 1.1 200 OK Expires: Fri , 1 Jan 2030 Cache-Control: public

This will instruct browser to cache the response till Jan 2030. As long as you make the same Xml Http call with the same parameters, you will get cached response from the computer and no call will go to the server. There are more advanced ways to get further control over response caching. For example, here is a header which will instruct browser to cache for 60 seconds but do contact server and get a fresh response after 60 seconds. It will also prevent proxies from returning cached response when browser local cache expires after 60 seconds.

HTTP/ 1.1 200 OK Cache-Control: private , must-revalidate , proxy-revalidate , max-age = 60

Let's try to produce such response headers from ASP.NET web service call:

This will result in the following response headers:

Expires header is set properly. But the problem is with Cache-Control. It is showing "max-age" is set to zero which will prevent browser from doing any kind of caching. If you seriously want to prevent caching, you should emit such cache-control header. Looks like exactly the opposite thing happened.

There's a bug in ASP.NET 2.0 that you cannot change "max-age" header. As max-age is set to zero, ASP.NET 2.0 sets Cache-Control to private because max-age = 0 means no cache needed. So, there's no way you can make ASP.NET 2.0 return proper headers which caches the response.

Time for a hack. After decompiling the code of HttpCachePolicy class (Context.Response.Cache object's class), I found the following code:

Somehow, this._maxAge is getting set to zero and the check: "if (!this._isMaxAgeSet || (delta < this._maxAge))" is preventing it from getting set to a bigger value. Due to this problem, we need to bypass the SetMaxAge function and set the value of the _maxAge field directly using Reflection.

This will return the following headers:

Now max-age is set to 60 and thus browser will cache the response for 60 seconds. If you make the same call again within 60 seconds, it will return the same response. Here's a test output which shows the date time returned from the server:

The client side code is like this:

function cachedHttpGet() { WebService.CachedGet( { useGetMethod: true , onMethodComplete: function (result) { debug.dump(result); } } ); }

Here you see, the response is cached for 60 seconds and after the time elapsed, there was a server call made and new date was returned. That response was again cached for 60 seconds.

Published Sat, Sep 23 2006 18:04 by omar
Filed under:

Comments

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Saturday, September 23, 2006 6:00 PM by Christoph Janz
Great series, Omar, congrats! Don't give away too much! ;-)

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, November 16, 2006 8:15 AM by Giulio

Using HTTP GET for web service in ASP.NET AJAX Beta 2 doesn't seem to work, has anyone else found this?

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Friday, November 17, 2006 4:20 AM by Paul

This is a really great article, many thanks for spending the time sharing this info.

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, November 28, 2006 8:12 PM by Simone Busoli

Hi Omar, as far as I've tested appending the expires header is enough for the response to be cached on the client, isn't it?

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, November 28, 2006 10:28 PM by Omar AL Zabir

It does not work in Webservices.

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Monday, December 04, 2006 6:14 AM by Giulio

OK, reading your new post on CodeProject I see why web services via get didn't work in ASP.NET AJAX Beta 2. You need to set [ScriptMethod(UseHttpGet=true)] and [WebOperation(true)] is no longer necessary.

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, March 06, 2007 12:12 AM by allrigo.

Very bad one...try to implement reflection.

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Saturday, May 05, 2007 7:35 AM by ryan

I have this working when using IIS 5.1. However, when I put this same logic on my Server 2003 production server it no longer works. I think it has something to do with IIS 6. Has anyone else seen this problem? If so how did you resolve the issue?

# Caching large HTML elements in the Browser&#8217;s document cache &laquo; Damian Mehers Blog

Pingback from  Caching large HTML elements in the Browser&#8217;s document cache &laquo; Damian Mehers Blog

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Friday, August 03, 2007 9:56 PM by Trahktengerts

modelwatches.biz/.../chinese-rolex-replica.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, August 07, 2007 7:24 PM by Hillary

replicawatchesworld.info/replica-watches$99-fake-rolex-watches.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, August 30, 2007 1:07 PM by Zigman

undag.biz/.../fake-chanel-handbag-stores-in-calgary,-alberta.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, September 04, 2007 3:58 PM by Zigman

wiund.biz/.../swiss-fake-rolex-watches.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Saturday, October 20, 2007 11:53 AM by goblin

[*map/map_index_cnx2_11.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Saturday, October 20, 2007 11:53 AM by goblin

[*map/map_index_cnx2_11.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 12:21 AM by goblin

[*map/map_index_cnx2_12.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 12:21 AM by goblin

[*map/map_index_cnx2_12.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:15 PM by goblin

[*map/map_index_cnx2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:15 PM by goblin

[*map/map_index_cnx2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:15 PM by goblin

[*map/map_index_cnx2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:15 PM by goblin

[*map/map_index_cnx2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:15 PM by goblin

[*map/map_index_cnx2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:15 PM by goblin

[*map/map_cnc2_13_mordy.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_cnc2_13_mordy.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_cnc2_13_mordy.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_cnc2_13_mordy.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_cnc2_13_mordy.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_index_cne2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_index_cne2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:16 PM by goblin

[*map/map_index_cne2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:17 PM by goblin

[*map/map_index_cne2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, October 21, 2007 4:17 PM by goblin

[*map/map_index_cne2_13.txt||10||r||1|| @]

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Friday, November 09, 2007 8:37 AM by keepsake

faugenaboy.com/.../christmas-homemade-food-gifts.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Saturday, November 10, 2007 8:00 AM by littlebo

savingmemoney.com/.../replica-veneta-handbags.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Monday, November 12, 2007 6:54 PM by iceboxom

flowersshopping.info/.../fall-flowers-wedding-arrangements.php

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Friday, January 04, 2008 4:03 AM by hipnoj

<a href= http://index1.krewor.com >jodo upskirt gameshow</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, January 06, 2008 6:05 AM by klira

<a href= http://index1.thejintro.com >strap on</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, January 06, 2008 6:05 AM by klira

<a href= http://index1.thejintro.com >strap on</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Sunday, January 06, 2008 6:41 PM by klira

<a href= http://index1.thewertool.com >fieldcrest farm</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Monday, January 07, 2008 1:19 AM by klira

<a href= http://index1.lopwersite.com >ma.medical association</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, January 08, 2008 4:53 AM by klira

<a href= http://index1.elurker.com >combining ceramics fused glass</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, January 08, 2008 2:47 PM by klira

<a href= http://index1.themounter.com >sample resumes for human resource assistant</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Tuesday, January 08, 2008 2:47 PM by klira

<a href= http://index1.themounter.com >sample resumes for human resource assistant</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Wednesday, January 09, 2008 5:34 PM by klira

<a href= http://index1.wentsite.com >girl hockey players</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Wednesday, January 09, 2008 11:42 PM by klira

<a href= http://index1.dosmounter.com >sexual addictions signs</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Wednesday, January 09, 2008 11:42 PM by klira

<a href= http://index1.dosmounter.com >sexual addictions signs</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, January 10, 2008 7:02 AM by klira

<a href= http://index1.emounter.com >spirit of woman of california fresno ca</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, January 10, 2008 12:08 PM by Vere-cm

<a href= http://index1.yritum.com >federal long shoreman act</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, January 10, 2008 12:08 PM by Vere-cm

<a href= http://index1.yritum.com >federal long shoreman act</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, January 10, 2008 12:08 PM by Vere-cm

<a href= http://index1.yritum.com >federal long shoreman act</a>

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Wednesday, June 10, 2009 8:34 PM by gas powered scooters

Best  extremely cheap gas powered scooters

www.world66.com/.../gas_powered_scoote

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, June 11, 2009 2:43 AM by paris hilton sex tape

Only Download paris hilton sex tape

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Thursday, June 11, 2009 6:40 PM by r kelly sex tape

Very good guys r kelly an kim sex tape www.youtube.com/.../rkellytapesex

# re: Atlas 7: Caching web service response on browser and save bandwidth significantly

Friday, June 12, 2009 12:57 AM by kristin davis sex tape

Is it real to see free kristin davis sex tape

Leave a Comment

(required) 
(required) 
(optional)
(required)