This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Ajax and the Caching issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax and the Caching issue" Watch "Ajax and the Caching issue" New topic
Author

Ajax and the Caching issue

Dinesh Sundrani
Ranch Hand

Joined: Mar 21, 2006
Posts: 78
Hi Authors,

I have a Ajax code that calculates the time, the Web Service call took to fetch the data from the datatabse and display it on the GUI [i.e the Turnaround Time] on a respective div/iframe element.
But sometimes what I have usually observed is that the time is being cached and even though the total process took around 8.35345 seconds, It actually displays the old time [time it took for the previous request] which was for ex say 9.2 seconds. So is it that the browser is caching my variables or is that a overall Ajax Caching issue. Does someone else also have had the same issue? Does this Book cover Effective strategies of dealing with Caching and improving the cache-hit percentage. Is there a way to stop caching of my data in the browser or is there a way to refresh the stale data and keep it in sync with the new request?

Please suggest so that I could propose the changes to Dev-Team!!

Regards,
Dinesh Sundrani
*******************
SCJP1.5 [86%]
SCWCD [95%]
SCDJWS [98%]
*******************
[ July 26, 2007: Message edited by: Bear Bibeault ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I only ever see this in IE and it's a fairly well known issue if you hit google on it. The solution I use is something like this..

Caveat is this code is Java. You'll need to adjust to your language of choice.


And then forward the request back to the client. The reason you need to do this in code is because IE is actually caching the HTML as a page that comes back so even if you added the meta tags to your HTML page, IE will never see them, as it pulls the cached page rather than the newly acquired page.

HTH


GenRocket - Experts at Building Test Data
Alexei White
author
Greenhorn

Joined: Jul 20, 2007
Posts: 14
This is a uniquely Internet Explorer issue. The solution we prefer is to use the cache-buster technique.. which is to append a unique string to the end of your XHR url for every request. Eg:



Alexei White<br />Nitobi Software<br />-------------------<br /><a href="http://www.nitobi.com" target="_blank" rel="nofollow">www.nitobi.com</a><br />Enterprise Ajax Book <a href="http://www.enterpriseajax.com" target="_blank" rel="nofollow">http://www.enterpriseajax.com</a><br />f. 604.985.9287
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Alexei White:
This is a uniquely Internet Explorer issue. The solution we prefer is to use the cache-buster technique.. which is to append a unique string to the end of your XHR url for every request. Eg:



By solution, don't you mean hack? This is well documented out in google land as well for dealing with IE caching images that tend to change. You can put a random number on the end of the image src to force IE to recognize it's a new image. The same applies for other types of requests as well, obviously.

I only prefer my suggestion because it's using specifications (yes, even if they are just for IE) to make the browser play nice rather than a hack or trick. But it's purely preference in this case.
Dinesh Sundrani
Ranch Hand

Joined: Mar 21, 2006
Posts: 78
Cool, I googled my concern and I found the exact same solution as Gregg suggested me, Thanks a ton!!

Regards,
Dinesh Sundrani
*******************
[SCJP 1.5 - 86%]
[SCWCD - 95%]
[SCDJWS - 98%]
*******************
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

Eric
 
wood burning stoves
 
subject: Ajax and the Caching issue
 
Similar Threads
Javascript based refresh
storing variable so it can be used by 2 servlets
Question to authors: AJAX caching
The 'Resend' issue.
Servlet - POI - Excel