tags:

views:

310

answers:

5

I have an ajax application where the client might lookup bunch of data frequently(say by key stroke), the data gets updated on the server side once or twice a day at fixed times by a demon process. To avoid visiting the server frequently, I store the data in a xml file, so the client downloads it once when the page first loads, then look the data up from local data file via javascript. But the user might load the page shortly before the changes, then start using it without ever refreshing the page, so the data file never gets updated, hence keep telling user the new data is not available. How do I solve this issue?

A: 

If the data load is not very large... Include the data in the main document as an XML island. Either form it in document generation (aspx, php, whatever) or fill in (via ajax calls) a reserved document node upon loading. This way, your user always has the latest data, you do not have to worry about caching, and life is much simpler.

If it is large, fill in that node as needed via ajax calls.

CMB
when you say xml island, do you mean make the xml data and part of the main document dom?the data in my case is not small(at least 1mb)
The gist of this technique is just to hide the data in a document node. Don't worry about specifically using XML. Choose your own format. (XML is very much more verbose than quite a few other formats.) However, the term 'XML Island' is a well known pattern and will lead you to many examples.As far as the data...1 MB of data is not that large when it is delivered as part of the initial document as most servers compress requested documents... especially when the user only acquires the data on first load.Good luck
CMB
A: 

One obvious option is to add some AJAX that polls the server every x minutes. If the data needs refreshing just show a non-blocking message somewhere obvious on the page notifying the user that fresh data is available and provide a link to refresh the page. As an extra you might want to provide a button for the user to click if they want to check for fresh data (rather than waiting for x minutes to elapse) themselves.

If you use a HEAD request you can just check the last-modified header.

rojoca
A: 

You said that the update time is FIXED? So when the user visit your page SHORTLY before the update time to come, you can set a javascript variable to you page that indicate how many minutes, for example, until the next update, and run a client-side timer such as:

timer = { run: function() { if( now + minuteToUpdate > updateTime - startVisitTime ) {

        // make ajax request here to update XML file
    }
},
interval: //you can determine this since this will run in client-side

}

Do not you POLLING in this stiuation because it's waste and stressed to call server every time.

You can set some SESSION variable to help this run better and more exactly

Justin

thethanghn
+2  A: 

You should set the appropriate HTTP cache headers for that generated XML file so any client request past that time will get the new version and cache it locally, like any other static content.

An HTTP HEAD call will get you the last-modified header, which you can use to determine if there is a need to update.
Drew Stephens
how do I set the appropriate http cache headers for the xml file on the server side? does this mean the xml file has to be served through dynamic server code?
also is the HTTP HEAD call done via javascript? can you give some detail?
Since you update that XML twice a day, I would go serving it through an http handler and set it to expire every 12 hours (assuming the update takes place every 12 hours). By "http call done via js" I think you mean some ajax call in your client code to request that xml, so, yes, it'll download and cache on the client for further requests for the next 12 hours.
No need for the HTTP HEAD as it's as fast in the case of not being modified to receive a 304 from the GET, and faster in the case where you do need to download to receive a 200 from the GET, having skipped receiving it from a HEAD.
Jon Hanna
A: 

What runtime said...

Or, since update times are fixed and infrequent, then when you serve your XML, also include a cache expiration time as an element or custom header. This way, if your user visits the site 1 minute before the XML update, you can code your client to expire its cache and update itself on the next request made after that 1 minute mark.

Justin Johnson
so this means the xml file has to be served by server code which sends an expiration header prior to sending the xml data, right? Can I get the expiration header from client side(javascript?)? why do I need to make it the cache expiration time as an element or custom header?
You need to return the cache expiration time as a custom header or as an element in the normal XML response so that JavaScript can use it. And yes, you can get headers from AJAX requests in JavaScript.
Justin Johnson